Android按钮边框动态

Android按钮边框动态,android,button,border,Android,Button,Border,我有一个按钮,没有任何文本只有背景色。在按钮的onClick()事件中,我需要设置没有xml规范的按钮边框。我尝试将渐变矩形形状作为背景可绘制添加到按钮,但该按钮不适合我的布局 如何为按钮设置特定颜色的边框 这是我的密码 Button btnBlackColor=new Button(this); int mButtonWidth=100; btnBlackColor.setWidth(mButtonWidth); btnBlackColor.setHeight(

我有一个按钮,没有任何文本只有背景色。在按钮的
onClick()
事件中,我需要设置没有
xml
规范的按钮边框。我尝试将渐变矩形形状作为背景
可绘制
添加到按钮,但该按钮不适合我的布局

如何为按钮设置特定颜色的边框

这是我的密码

    Button btnBlackColor=new Button(this);
    int mButtonWidth=100;
    btnBlackColor.setWidth(mButtonWidth);
    btnBlackColor.setHeight(mButtonWidth);
    btnBlackColor.setBackgroundColor(Color.BLACK);  

    btnBlackColor.setOnClickListener(new OnClickListener()
    {
      public void onClick(View v)
      {
        GradientDrawable btnShape = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]{Color.BLUE,Color.LTGRAY});
        btnShape.setCornerRadius(0);
        btnShape.setSize(mButtonWidth, mButtonWidth);
        btnShape.setBounds(10, 10, mButtonWidth, mButtonWidth);
        ClipDrawable btnClip = new ClipDrawable(btnShape, Gravity.LEFT,ClipDrawable.HORIZONTAL);

        btnShape = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]{Color.BLACK, Color.DKGRAY});
        btnShape.setCornerRadius(0); 
        btnShape.setSize(mButtonWidth, mButtonWidth);
        btnShape.setBounds(5, 5, mButtonWidth, mButtonWidth);


        LayerDrawable btnLayer= new LayerDrawable(new Drawable[]{btnShape, btnClip});

        btnBlackColor.setBackgroundDrawable(btnLayer); 
      }
    });

在xml背景中使用选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"><!-- pressed -->
       <shape>...</shape>
    <item android:state_focused="true"
          android:drawable="@drawable/button_focused" /> <!-- focused -->
    <item android:state_hovered="true"
          android:drawable="@drawable/button_focused" /> <!-- hovered -->
    <item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>

    
...
     
     
     
burton的每个状态都可能是一个形状,请查看您可以修改的属性:

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"] >
    <corners
        android:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />
    <gradient
        android:angle="integer"
        android:centerX="integer"
        android:centerY="integer"
        android:centerColor="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type=["linear" | "radial" | "sweep"]
        android:useLevel=["true" | "false"] />
    <padding
        android:left="integer"
        android:top="integer"
        android:right="integer"
        android:bottom="integer" />
    <size
        android:width="integer"
        android:height="integer" />
    <solid
        android:color="color" />
    <stroke
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"
        android:dashGap="integer" />
</shape>

    
    
    
    
    
    

通过代码,您可以使用
StateListDrawable
创建选择器,对于形状,请使用
DrawableShape
查看以下链接:

找到了解决方案

    GradientDrawable drawable = new GradientDrawable();
    drawable.setShape(GradientDrawable.RECTANGLE);
    drawable.setStroke(5, Color.MAGENTA);
    drawable.setColor(Color.BLACK);
    btnBlackColor.setBackgroundDrawable(drawable);

使用这些值并设置为按钮背景可绘制。现在,按钮的边框为洋红色。

“我有一个按钮,没有任何文本,只有背景色”——这既不像按钮,也不像按钮,按钮之所以看起来对点击做出响应,是因为它的背景是
StateListDrawable
。在没有xml规范的情况下,如何添加StateListDrawable?
StateListDrawable
是一个Java类。欢迎您创建它的实例,并根据需要配置这些实例。话虽如此,由于约99.44%的
StateListDrawable
用户是通过XML进行管理的,因此您可能会发现通过Java进行管理的示例相对较少。我需要在不使用XML文件的情况下进行设置。如何设置这些值?您需要做什么?setBackgroundDrawable不适用于您?单击按钮时,如何突出显示按钮。?我希望它可能是一个边界。请看新版的答案。可以通过笔划属性修改边框。是的,但是我正在努力通过代码添加那些指定的参数。你能帮帮我吗?太好了,这对我也有帮助+1如何设置图像,然后向其添加笔划?