如何设置顶部带有文本的 AppCompatRadioButton 并以编程方式更改色调颜色?
How to set AppCompatRadioButton with text on top and change the tint color programatically?
我试图在 AppCompatRadioButton
中将文本置于顶部并以编程方式更改 setSupportButtonTintList
。我通过自定义 AppCompatRadioButton 成功地将文本顶部对齐。但现在 setSupportButtonTintList 无法使用此功能,无需自定义 setSupportButtonTintList 即可正常工作。
acticivity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:padding="@dimen/activity_vertical_margin"
tools:showIn="@layout/activity_main">
<android.support.v7.widget.AppCompatRadioButton
android:id="@+id/radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:text="Top Text"
android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
android:gravity="center" />
</RelativeLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
AppCompatRadioButton appCompatRadioButton = (AppCompatRadioButton) findViewById(R.id.radio);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int myColor = Color.parseColor("#4caf50");
appCompatRadioButton.setSupportButtonTintList(ColorStateList.valueOf(myColor));
}
}
}
您正在为按钮应用色调,但是因为您已将按钮设置为空 xml。
android:button="@null"
所以你不能对按钮应用色调。您正在使用 drawableBottom 作为单选按钮。所以你必须对底部的可绘制对象应用色调。
android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
要在 drawableBottom 上应用色调,您必须先获取底部可绘制对象,然后再应用色调。
AppCompatRadioButton appCompatRadioButton = (AppCompatRadioButton) findViewById(R.id.radio);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int myColor = Color.parseColor("#4caf50");
//appCompatRadioButton.setSupportButtonTintList(ColorStateList.valueOf(myColor)); // Your code applying tint to button
appCompatRadioButton.getCompoundDrawables()[3].setColorFilter(myColor, PorterDuff.Mode.SRC_ATOP); // Applying tint to drawable at bottom. '3' to get drawable at bottom
}
祝你好运:)
我试图在 AppCompatRadioButton
中将文本置于顶部并以编程方式更改 setSupportButtonTintList
。我通过自定义 AppCompatRadioButton 成功地将文本顶部对齐。但现在 setSupportButtonTintList 无法使用此功能,无需自定义 setSupportButtonTintList 即可正常工作。
acticivity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:padding="@dimen/activity_vertical_margin"
tools:showIn="@layout/activity_main">
<android.support.v7.widget.AppCompatRadioButton
android:id="@+id/radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:text="Top Text"
android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
android:gravity="center" />
</RelativeLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
AppCompatRadioButton appCompatRadioButton = (AppCompatRadioButton) findViewById(R.id.radio);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int myColor = Color.parseColor("#4caf50");
appCompatRadioButton.setSupportButtonTintList(ColorStateList.valueOf(myColor));
}
}
}
您正在为按钮应用色调,但是因为您已将按钮设置为空 xml。
android:button="@null"
所以你不能对按钮应用色调。您正在使用 drawableBottom 作为单选按钮。所以你必须对底部的可绘制对象应用色调。
android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
要在 drawableBottom 上应用色调,您必须先获取底部可绘制对象,然后再应用色调。
AppCompatRadioButton appCompatRadioButton = (AppCompatRadioButton) findViewById(R.id.radio);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int myColor = Color.parseColor("#4caf50");
//appCompatRadioButton.setSupportButtonTintList(ColorStateList.valueOf(myColor)); // Your code applying tint to button
appCompatRadioButton.getCompoundDrawables()[3].setColorFilter(myColor, PorterDuff.Mode.SRC_ATOP); // Applying tint to drawable at bottom. '3' to get drawable at bottom
}
祝你好运:)