【问题标题】:Android button border dynamicallyAndroid按钮边框动态
【发布时间】:2013-03-05 14:32:51
【问题描述】:

我有一个按钮,没有任何文字,只有背景颜色。在按钮的onClick() 事件中,我需要设置没有xml 规范的按钮边框。我尝试将渐变矩形形状作为背景drawable 到对我的布局不灵活的按钮。

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

这是我的代码。

    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); 
      }
    });

【问题讨论】:

  • “我有一个按钮,没有任何文本,只有背景颜色”——这看起来和行为都不像一个按钮,因为使按钮看起来响应点击的原因是它的背景作为StateListDrawable.
  • 如何在没有 xml 规范的情况下添加 StateListDrawable?
  • StateListDrawable 是一个 Java 类。欢迎您创建它的实例并根据需要配置这些实例。话虽如此,由于大约 99.44% 的 StateListDrawable 用户通过 XML 进行操作,因此您可能会发现通过 Java 进行管理的示例相对较少。

标签: android button border


【解决方案1】:

找到解决方案。

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

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

【讨论】:

  • 太好了,对我也有帮助。 +1
  • 如何设置图像,然后添加描边?
【解决方案2】:

在您的 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 请查看以下链接:

http://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html

http://developer.android.com/reference/android/graphics/drawable/ShapeDrawable.html

【讨论】:

  • 我需要在没有 xml 文件的情况下进行设置。如何设置这些值?
  • 您到底需要做什么? setBackgroundDrawable 对你没用?
  • 单击按钮时,如何突出显示按钮。?我更喜欢它可能是一个边界。
  • 请看新版的答案。边框可以通过 stroke 属性进行修改。 BR
  • 是的,但是我正在努力通过代码添加那些指定的参数。你能帮帮我吗?
猜你喜欢
  • 2011-12-03
  • 2016-02-24
  • 2023-04-09
  • 2016-10-20
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 2013-10-01
相关资源
最近更新 更多