BoD*_*BoD 79 android tint android-layout
我的活动中有一个Button,我希望它有我主题的强调色.而不是像我们必须做的那样制作我自己的可绘制的Lollipop,我自然会想要使用新backgroundTint
属性.
<Button
android:id="@+id/btnAddCode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/accent"
android:text="@string/addressInfo_edit_addCode" />
Run Code Online (Sandbox Code Playgroud)
不幸的是它没有效果,按钮保持灰色.
我尝试了不同的值backgroundTintMode
,但没有改变任何东西.
我也尝试在我的Activity中以编程方式进行,它没有改变任何东西.
addCodeView.findViewById(R.id.btnAddCode).setBackgroundTintList(
getResources().getColorStateList(R.color.accent));
Run Code Online (Sandbox Code Playgroud)
为什么我的色彩被忽略了?
编辑:只是为了澄清,我确实在Lollipop设备上进行测试.其他小部件(例如EditText)正确并自动着色.
Sni*_*kow 114
就像BoD所说的那样,在Lollipop 5.0(API级别21)中为Button的背景着色毫无意义.
Lollipop 5.1(API级别22)似乎通过更改btn_mtrl_default_shape.xml(以及其他文件)来解决此问题:https://android.googlesource.com/platform/frameworks/base/+/6dfa60f33ca6018959ebff1efde82db7d2aed1e3%5E !/#F0
新的支持库(版本22.1+)为许多组件添加了向后兼容的着色支持,包括AppCompatButton!
不幸的是,该android:backgroundTint
属性仍然无法工作(也许我做错了) - 所以你必须使用设置ColorStateList
代码setSupportBackgroundTintList()
.很高兴看到android:backgroundTint
未来的支持.更新:Marcio Granzotto评论说app:backgroundTint
可以在AppCompatButton上运行!请注意,它app:
不是android:
,因为它在app/library中.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<AppCompatButton
android:id="@+id/mybutton"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Testing, testing"
app:backgroundTint="#ff00ff"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
如果您让它继承,您的活动将自动膨胀AppCompatButton
而不是正常.Button
AppCompatActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AppCompatButton v = (AppCompatButton) findViewById(R.id.mybutton);
ColorStateList csl = new ColorStateList(new int[][]{new int[0]}, new int[]{0xffffcc00});
v.setSupportBackgroundTintList(csl);
}
}
Run Code Online (Sandbox Code Playgroud)
你当然应该ColorStateList
从色彩资源中获取,但我很懒,所以......
哦,不要忘记将你的应用主题基于其中一个Theme.AppCompat
主题,否则compat视图将非常非常悲伤......;)
这适用于2.3.7(Gingerbread MR1)和5.0(Lollipop'Classic').
BoD*_*BoD 30
似乎将波纹绘制着色是没有意义的(并且按钮的默认背景是可绘制的波纹).
事实上,在查看平台的默认按钮drawable之后,我找到了"正确"的方法来做到这一点:你必须在你的主题中定义它:
<item name="android:colorButtonNormal">@color/accent</item>
Run Code Online (Sandbox Code Playgroud)
(当然这仅适用于21+级.)
警告:因为这是在主题中定义的,所以这将使用给定的颜色用于所有按钮(至少使用该主题的活动中的所有按钮.)
作为奖励,您还可以通过定义以下内容来更改波纹颜色:
<item name="android:colorControlHighlight">@color/accent_ripple</item>
Run Code Online (Sandbox Code Playgroud)
小智 22
要解决与Android 5.0.x上的着色相关的问题,我使用以下内容:
public static void setButtonTint(Button button, ColorStateList tint) {
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP && button instanceof AppCompatButton) {
((AppCompatButton) button).setSupportBackgroundTintList(tint);
} else {
ViewCompat.setBackgroundTintList(button, tint);
}
}
Run Code Online (Sandbox Code Playgroud)
它仅对API 21使用支持方法,对所有其他情况使用ViewCompat.
Car*_*rau 19
我通常使用PorterDuff动态地执行它:
mbutton = (Button) findViewById(R.id.mybutton);
mbutton.getBackground().setColorFilter(anycolor, PorterDuff.Mode.MULTIPLY);
Run Code Online (Sandbox Code Playgroud)
dra*_*eet 19
只需使用app:backgroundTint
代替android:backgroundTint
,色彩将在棒棒糖下面生效.原因是AppCompatActivity
用于AppCompatViewInflater
自动将Button或TextView更改为AppCompatButton或AppCompatTextView,然后app:backgroundTint
生效.
Sah*_*oor 16
通过API 27在API 19上进行测试
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.AppCompatButton
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/retry"
android:textColor="@android:color/white"
app:backgroundTint="@android:color/holo_red_dark" />
Run Code Online (Sandbox Code Playgroud)
产生输出为 -
我认为你需要有android:background
设置,使android:backgroundTint
工作.
为了更准确,我的猜测是你不能backgroundTint
从Material themes定义的默认按钮背景RippleDrawable
.