我如何使用背景色调ImageButton或AppCompatImageButton?请回答XML和Java.我找到了一个说我最常用的java解决方案setBackgroundTintList(),但是当我使用它时,背景总是显示为蓝色而且在点击时不会改变.这是我尝试过的:
mButtonBold.setSupportBackgroundTintList(new ColorStateList(
new int[][]{EMPTY_STATE_SET, PRESSED_ENABLED_STATE_SET},
new int[]{Color.BLUE, Color.GREEN}
))
Run Code Online (Sandbox Code Playgroud)
小智 10
首先你的数组顺序是错误的.默认值必须是最后一个状态,所以使用这个:
mButtonBold.setSupportBackgroundTintList(new ColorStateList(
new int[][]{PRESSED_ENABLED_STATE_SET,EMPTY_STATE_SET},
new int[]{Color.GREEN,Color.BLUE}
))
Run Code Online (Sandbox Code Playgroud)
XML
<android.support.v7.widget.AppCompatImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_done"
app:backgroundTint="@drawable/background"
app:backgroundTintMode="src_over"/>
Run Code Online (Sandbox Code Playgroud)
绘制/背景
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorAccent" android:state_pressed="true" />
<item android:color="@color/colorPrimary" />
</selector>
Run Code Online (Sandbox Code Playgroud)
JAVA
如果考虑布局是这样的:
<android.support.v7.widget.AppCompatImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_done" />
Run Code Online (Sandbox Code Playgroud)
然后:
AppCompatImageButton imageButton = (AppCompatImageButton)findViewById(R.id.imageButton);
imageButton.setSupportBackgroundTintList(new ColorStateList(
new int[][]{new int[]{android.R.attr.state_pressed},
new int[]{}
},
new int[]{Color.GREEN,Color.BLUE}
));
imageButton.setSupportBackgroundTintMode(PorterDuff.Mode.SRC_OVER);
Run Code Online (Sandbox Code Playgroud)
如果你打算使用SDK Widget使用android前缀而不是appwhere
xmlns:app="http://schemas.android.com/apk/res-auto"
Run Code Online (Sandbox Code Playgroud)
和
xmlns:android="http://schemas.android.com/apk/res/android"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20451 次 |
| 最近记录: |