【问题标题】:Set background color and use Drawable as background in Android在Android中设置背景颜色并使用Drawable作为背景
【发布时间】:2015-12-15 12:23:10
【问题描述】:

此代码用于动态创建按钮。问题是我想设置背景颜色并设置背景Drawables。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke android:width="1px" android:color="#696969"/>
</shape>

这是我想设置按钮背景颜色的类,然后我想使用我的 Drawable。

Button btnTag = new Button(alltable.this);
btnTag.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 
                       LinearLayout.LayoutParams.WRAP_CONTENT));
try {
    btnTag.setWidth(130);
    btnTag.setBackground(getResources().getDrawable(R.color.blue));

} catch (Exception e){
    e.printStackTrace();
}

【问题讨论】:

  • 'android:background' 应该在这里工作。它需要"@color/..."@drawable/..." 参数
  • 然后使用可绘制图层
  • 请清除你的答案@piotrek1543
  • 抱歉,我不知道您已经在使用btnTag.setBackground(getResources().getDrawable(R.color.blue));,因为您会注意到通过这种方法您也可以使用drawable (r.drawable.LOGO)
  • 如果您想为整个布局设置背景颜色,请转到布局 xml 文件并设置为主布局android:background 属性

标签: android android-layout button drawable


【解决方案1】:

在drawable(如files_bg.xml)文件夹中创建一个资源并将其设置为布局背景。

在图层列表中使用两项,一项用于形状的纯色,另一项用于位图。

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/totalFilesBgColor" />
        </shape>
    </item>
    <item>
        <bitmap
            android:src="@drawable/bg_icon"
            android:tileMode="disabled" />
    </item>
</layer-list> 

现在将 drawable 设置为布局中的背景或您使用它的任何地方。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/files_bg">
</LinearLayout>

【讨论】:

    【解决方案2】:

    这个文件(rectangle.xml)放在你的drawable文件夹中。

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
    <solid android:color="#FF4081"/>
    </shape>
    

    在你的代码中修改这一行。

    btnTag.setBackground(getResources().getDrawable(R.drawable.rectangle,null));\\API level 21 and higher, otherwise
     getResources().getDrawable(R.drawable.rectangle).
    

    【讨论】:

      【解决方案3】:

      有点晚了,但我遇到了同样的问题,我按照下面的描述解决了。

      首先获取你的drawable:

      Drawable d = getResources().getDrawable(R.drawable.shape_rectangle);
      

      然后将您的自定义颜色应用到它:

      d.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
      

      然后将其设置为视图/按钮的背景:

      btn.setBackground(d);
      

      【讨论】:

        【解决方案4】:

        这对我有用

        <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
          <item>
            <shape android:shape="rectangle">
                <solid android:color="@color/appColor" />
                <corners android:radius="@dimen/spacing_small"/>
            </shape>
           </item>
             <item android:drawable="@drawable/add_white"/>
        </layer-list>
        

        【讨论】:

          【解决方案5】:

          您可以创建一个新的可绘制对象并将其分配给背景。 即

          
          Drawable drawable = new GradientDrawable();
          drawable.shape = GradientDrawable.RECTANGLE;
          drawable.setStroke(Utils.toPx(1), Color.parseColor("#696969") );
          btnTag.setBackground(drawable);
          

          【讨论】:

            猜你喜欢
            • 2013-08-04
            • 1970-01-01
            • 2012-01-20
            • 1970-01-01
            • 2021-06-29
            • 2023-03-25
            • 1970-01-01
            • 1970-01-01
            • 2010-11-08
            相关资源
            最近更新 更多