I want to show a radio list inside an AlertDialog with custom styling, something like .
因此,我创建了一个自定义主题并将其作为参数提供给 AlertDialog.Builder 的构造函数。
这是显示对话框的代码:
private void showSortDialog() {
final CharSequence[] options = new String[] {"Relevance", "Newest"};
AlertDialog.Builder builder = new AlertDialog.Builder(getActivityReference(),
R.style.RadioDialogTheme);
builder.setTitle("Sort by");
builder.setSingleChoiceItems(options, -1, new DialogInterface.OnClickListener() {
. . . .
builder.create().show();
}
这是风格:
<style name="RadioDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:textColorAlertDialogListItem">@drawable/radiobutton_textcolor_selector
</item>
<item name="android:listChoiceIndicatorSingle">@drawable/apptheme_btn_radio_holo_light
</item>
</style>
我能够找到一些在我的样式中添加的属性,以根据单选按钮/文本的状态更改其颜色。但我无法自定义文本外观(我想更改大小、提供填充等)。
我确信有一些属性可以用来设置文本样式,但我找不到它。谁能帮我解决这个问题吗?谢谢。
我想这几乎是你需要的:
fun showSortDialog(context: Activity) {
val options = arrayOf(
"Relevance",
"Price - Low to High",
"Price - High to Low",
"Newest"
)
val builder = AlertDialog.Builder(context, R.style.MultiChoiceAlertDialog)
val view = context.layoutInflater.inflate(R.layout.dialog_custom, null, false)
val radioGroup = view.findViewById<RadioGroup>(R.id.radiogroup)
val purpleColor = ContextCompat.getColor(context, R.color.purple)
val radioStyle = ContextThemeWrapper(radioGroup.context, R.style.MyRadioButton)
for (option in options) {
val radioButton = RadioButton(radioStyle)
radioButton.setText(option)
radioGroup.addView(radioButton)
}
radioGroup.setOnCheckedChangeListener { group, checkedId ->
for (child in radioGroup.children) {
child as RadioButton
if (child.id == checkedId) {
child.setTextColor(purpleColor)
} else {
child.setTextColor(Color.BLACK)
}
}
}
builder.setView(view)
builder.show()
}
styles:
<style name="YourAlertDialog.Button" parent="Widget.MaterialComponents.Button.TextButton">
<item name="android:textColor">@color/colorPrimary</item>
<item name="android:textSize">20sp</item>
<item name="android:textAllCaps">false</item>
<item name="android:gravity">left</item>
<item name="android:letterSpacing">0</item>
</style>
<style name="MultiChoiceAlertDialog" parent="Theme.MaterialComponents.Light.Dialog.Alert">
<item name="buttonBarPositiveButtonStyle">@style/YourAlertDialog.Button</item>
<item name="buttonBarNegativeButtonStyle">@style/YourAlertDialog.Button</item>
<item name="buttonBarNeutralButtonStyle">@style/YourAlertDialog.Button</item>
<item name="android:background">#fff</item>
<item name="android:textColorPrimary">#000</item>
<item name="android:textColor">@drawable/selector_custom</item>
<item name="android:colorActivatedHighlight">#0f0</item>
<item name="android:colorControlActivated">#00f</item>
<item name="colorControlNormal">@color/gray</item>
<item name="colorControlActivated">@color/purple</item>
<item name="dialogCornerRadius">8dp</item>
</style>
<style name="MyRadioButton" parent="Theme.AppCompat.Light">
<item name="colorControlNormal">@color/gray</item>
<item name="colorControlActivated">@color/purple</item>
</style>
和自定义布局(dialog_custom.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="Sort by"
android:textColor="#000"
android:textSize="20sp" />
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RadioGroup>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)