如何使用布局作为背景来设置按钮的背景颜色

huangapple go评论109阅读模式
英文:

How to set the background color of a button using a layout as the background

问题

layout_bg.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">
  3. <solid android:color="#FFFFFF"/>
  4. <!--<stroke android:width="3dp" android:color="#B1BCBE" />-->
  5. <corners android:radius="10dp"/>
  6. <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
  7. </shape>

main_activity.xml

  1. <ImageButton
  2. android:background="@drawable/layout_bg"
  3. android:src="@drawable/myicon"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content"
  6. android:tint="@android:color/black"
  7. android:minWidth="80.0dp"
  8. android:minHeight="80.0dp"
  9. android:padding="10px"
  10. android:scaleType="fitCenter"
  11. android:layout_alignParentBottom="true"
  12. android:id="@+id/btnFoo"
  13. android:layout_marginLeft="91.0dp"
  14. />

I am setting the background of my ImageButton to a layout so I can have dynamic rounded corners as you can see in this image:

如何使用布局作为背景来设置按钮的背景颜色

However, I need to change the background color of just that button to another solid color such as red.

If I set the background color with btnFoo.SetBackgroundColor(Color.Red), then the rounded corners are gone.

I assume this is because it overwrote the background attribute with a solid color value rather than using the layout_bg.xml file.

How can I set the background color of this specific ImageButton without affecting all the ImageButtons in my main_activity that use layout_bg as the background?

英文:

layout_bg.xml

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;shape xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
  3. &lt;solid android:color=&quot;#FFFFFF&quot;/&gt;
  4. &lt;!--&lt;stroke android:width=&quot;3dp&quot; android:color=&quot;#B1BCBE&quot; /&gt;--&gt;
  5. &lt;corners android:radius=&quot;10dp&quot;/&gt;
  6. &lt;padding android:left=&quot;0dp&quot; android:top=&quot;0dp&quot; android:right=&quot;0dp&quot; android:bottom=&quot;0dp&quot; /&gt;
  7. &lt;/shape&gt;

main_activity.xml

  1. &lt;ImageButton
  2. android:background=&quot;@drawable/layout_bg&quot;
  3. android:src=&quot;@drawable/myicon&quot;
  4. android:layout_width=&quot;wrap_content&quot;
  5. android:layout_height=&quot;wrap_content&quot;
  6. android:tint=&quot;@android:color/black&quot;
  7. android:minWidth=&quot;80.0dp&quot;
  8. android:minHeight=&quot;80.0dp&quot;
  9. android:padding=&quot;10px&quot;
  10. android:scaleType=&quot;fitCenter&quot;
  11. android:layout_alignParentBottom=&quot;true&quot;
  12. android:id=&quot;@+id/btnFoo&quot;
  13. android:layout_marginLeft=&quot;91.0dp&quot;
  14. /&gt;

I am setting the background of my ImageButton to a layout so I can have dynamic rounded corners as you can see in this image:

如何使用布局作为背景来设置按钮的背景颜色

However, I need to change the background color of just that button to another solid color such as red.

If I set the background color with btnFoo.SetBackgroundColor(Color.Red), then the rounded corners are gone.

I assume because it overwrote the background attribute to be a solid color value rather than the layout_bg.xml file.

How can I set the background color of this specific ImageButton and not all the ImageButtons in my main_activity that use layout_bg as the background?

答案1

得分: 1

你需要创建另一个颜色为红色的可绘制对象,然后使用它:

  1. btnFoo.setBackgroundResource(R.drawable.red_drawable);
英文:

You need to make another drawable with the color Red then use it :

  1. btnFoo.setBackgroundResource(R.drawable.red_drawable);

答案2

得分: 0

  1. 使用材料按钮,所有功能已经内置。
  2. <com.google.android.material.button.MaterialButton
  3. android:layout_width="尺寸"
  4. android:layout_height="尺寸"
  5. android:backgroundTint="@color/colorPrimary"
  6. android:text="添加图片"
  7. android:textAllCaps="false"
  8. android:textColor="@color/white"
  9. app:cornerRadius="20dp"
  10. android:layout_marginRight="10dp"
  11. android:layout_marginLeft="20dp"/>
英文:

use material button all functionality is built in already

  1. &lt;com.google.android.material.button.MaterialButton
  2. android:layout_width=&quot;size&quot;
  3. android:layout_height=&quot;size&quot;
  4. android:backgroundTint=&quot;@color/colorPrimary&quot;
  5. android:text=&quot;Add Images&quot;
  6. android:textAllCaps=&quot;false&quot;
  7. android:textColor=&quot;@color/white&quot;
  8. app:cornerRadius=&quot;20dp&quot;
  9. android:layout_marginRight=&quot;10dp&quot;
  10. android:layout_marginLeft=&quot;20dp&quot;/&gt;

答案3

得分: 0

你可以使用类似以下的代码:

  1. btnFoo.background.colorFilter =
  2. BlendModeColorFilterCompat.createBlendModeColorFilterCompat(
  3. color, BlendModeCompat.SRC_ATOP)

注意: 需要 androidx.core:core:1.2.0

作为替代,你也可以使用 MaterialButton

  1. <com.google.android.material.button.MaterialButton
  2. android:layout_width="80dp"
  3. android:layout_height="80dp"
  4. android:insetTop="0dp"
  5. android:insetBottom="0dp"
  6. app:icon="@drawable/ic_add_24px"
  7. app:iconGravity="textStart"
  8. app:iconPadding="0dp"
  9. app:iconSize="48dp"
  10. android:backgroundTint="@color/..."
  11. android:textColor="#FFFFFF"
  12. app:cornerRadius="10dp"/>

在这种情况下,你可以使用:

  1. button1.backgroundTintList = ContextCompat.getColorStateList(this, R.color....)
英文:

You can use something like:

  1. btnFoo.background.colorFilter =
  2. BlendModeColorFilterCompat.createBlendModeColorFilterCompat(
  3. color, BlendModeCompat.SRC_ATOP)

如何使用布局作为背景来设置按钮的背景颜色

Note: it requires androidx.core:core:1.2.0

As alternative you can use the MaterialButton :

  1. &lt;com.google.android.material.button.MaterialButton
  2. android:layout_width=&quot;80dp&quot;
  3. android:layout_height=&quot;80dp&quot;
  4. android:insetTop=&quot;0dp&quot;
  5. android:insetBottom=&quot;0dp&quot;
  6. app:icon=&quot;@drawable/ic_add_24px&quot;
  7. app:iconGravity=&quot;textStart&quot;
  8. app:iconPadding=&quot;0dp&quot;
  9. app:iconSize=&quot;48dp&quot;
  10. android:backgroundTint=&quot;@color/...&quot;
  11. android:textColor=&quot;#FFFFFF&quot;
  12. app:cornerRadius=&quot;10dp&quot;/&gt;

In this case just use:

  1. button1.backgroundTintList = ContextCompat.getColorStateList(this, R.color....)

如何使用布局作为背景来设置按钮的背景颜色

huangapple
  • 本文由 发表于 2020年8月20日 00:50:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63491551.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定