英文:
How to set the background color of a button using a layout as the background
问题
layout_bg.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<!--<stroke android:width="3dp" android:color="#B1BCBE" />-->
<corners android:radius="10dp"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
main_activity.xml
<ImageButton
android:background="@drawable/layout_bg"
android:src="@drawable/myicon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="@android:color/black"
android:minWidth="80.0dp"
android:minHeight="80.0dp"
android:padding="10px"
android:scaleType="fitCenter"
android:layout_alignParentBottom="true"
android:id="@+id/btnFoo"
android:layout_marginLeft="91.0dp"
/>
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
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<!--<stroke android:width="3dp" android:color="#B1BCBE" />-->
<corners android:radius="10dp"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
main_activity.xml
<ImageButton
android:background="@drawable/layout_bg"
android:src="@drawable/myicon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="@android:color/black"
android:minWidth="80.0dp"
android:minHeight="80.0dp"
android:padding="10px"
android:scaleType="fitCenter"
android:layout_alignParentBottom="true"
android:id="@+id/btnFoo"
android:layout_marginLeft="91.0dp"
/>
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
你需要创建另一个颜色为红色的可绘制对象,然后使用它:
btnFoo.setBackgroundResource(R.drawable.red_drawable);
英文:
You need to make another drawable with the color Red then use it :
btnFoo.setBackgroundResource(R.drawable.red_drawable);
答案2
得分: 0
使用材料按钮,所有功能已经内置。
<com.google.android.material.button.MaterialButton
android:layout_width="尺寸"
android:layout_height="尺寸"
android:backgroundTint="@color/colorPrimary"
android:text="添加图片"
android:textAllCaps="false"
android:textColor="@color/white"
app:cornerRadius="20dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="20dp"/>
英文:
use material button all functionality is built in already
<com.google.android.material.button.MaterialButton
android:layout_width="size"
android:layout_height="size"
android:backgroundTint="@color/colorPrimary"
android:text="Add Images"
android:textAllCaps="false"
android:textColor="@color/white"
app:cornerRadius="20dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="20dp"/>
答案3
得分: 0
你可以使用类似以下的代码:
btnFoo.background.colorFilter =
BlendModeColorFilterCompat.createBlendModeColorFilterCompat(
color, BlendModeCompat.SRC_ATOP)
注意: 需要 androidx.core:core:1.2.0
。
作为替代,你也可以使用 MaterialButton
:
<com.google.android.material.button.MaterialButton
android:layout_width="80dp"
android:layout_height="80dp"
android:insetTop="0dp"
android:insetBottom="0dp"
app:icon="@drawable/ic_add_24px"
app:iconGravity="textStart"
app:iconPadding="0dp"
app:iconSize="48dp"
android:backgroundTint="@color/..."
android:textColor="#FFFFFF"
app:cornerRadius="10dp"/>
在这种情况下,你可以使用:
button1.backgroundTintList = ContextCompat.getColorStateList(this, R.color....)
英文:
You can use something like:
btnFoo.background.colorFilter =
BlendModeColorFilterCompat.createBlendModeColorFilterCompat(
color, BlendModeCompat.SRC_ATOP)
Note: it requires androidx.core:core:1.2.0
As alternative you can use the MaterialButton
:
<com.google.android.material.button.MaterialButton
android:layout_width="80dp"
android:layout_height="80dp"
android:insetTop="0dp"
android:insetBottom="0dp"
app:icon="@drawable/ic_add_24px"
app:iconGravity="textStart"
app:iconPadding="0dp"
app:iconSize="48dp"
android:backgroundTint="@color/..."
android:textColor="#FFFFFF"
app:cornerRadius="10dp"/>
In this case just use:
button1.backgroundTintList = ContextCompat.getColorStateList(this, R.color....)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论