AlertDialog中的样式单选按钮和文本

时间:2015-10-21 12:59:38

标签: android android-alertdialog android-theme android-styles

我想在AlertDialog中显示一个带有自定义样式的广播列表,例如this

所以我创建了一个自定义主题,并将其作为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>

我能够找到一些我在我的风格中添加的属性,以根据状态更改单选按钮/文本的颜色。但是我无法自定义文本外观(我想改变大小,提供填充等)。

我确定有一些属性用于我可以设置文本样式,但我无法找到它。有人可以帮我这个吗?感谢。

3 个答案:

答案 0 :(得分:2)

我想几乎您需要:

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()
}

样式:

 <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>

screenshot

答案 1 :(得分:1)

首先,使对话框布局如下:

<androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/tv_title"
                android:layout_width="match_parent"
                android:layout_height="@dimen/_40sdp"
                android:layout_gravity="center|start"
                android:layout_marginStart="@dimen/_25sdp"
                android:layout_marginTop="@dimen/_20sdp"
                android:layout_marginEnd="@dimen/_25sdp"
                android:layout_marginBottom="@dimen/_10sdp"
                android:fontFamily="@font/lato_bold"
                android:gravity="center|start"
                android:text="@string/text_select_speciality"
                android:textColor="@color/color_dark_grey"
                android:textSize="@dimen/_20ssp" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rv_speciality_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/_15sdp"
                tools:listitem="@layout/item_speciality" />

然后像这样在Recyclerview中创建一个项目,以我的Checkbox为例,但您可以使用Radiobutton:

<androidx.appcompat.widget.AppCompatCheckBox
        android:id="@+id/rb_title"
        android:layout_width="match_parent"
        android:layout_height="@dimen/_40sdp"
        android:layout_marginStart="@dimen/_25sdp"
        android:layout_marginEnd="@dimen/_25sdp"
        android:button="@drawable/radio_button_selector"
        android:gravity="center|start"
        android:paddingStart="@dimen/_20sdp"
        android:paddingEnd="@dimen/_20sdp"
        android:text="@string/text_speciality"
        android:textColor="@color/color_dark_grey"
        android:textSize="@dimen/_14ssp" />

然后如下所示初始化对话框和Recyclerview:

private void showDialog() {
        dialog = new Dialog(LastActivity.this, R.style.DialogSlideAnim);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(false);
        dialog.setContentView(R.layout.dialog);
        Objects.requireNonNull(dialog.getWindow()).setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);

        if (dialog.getWindow() != null)
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        AppCompatTextView tv_title = dialog.findViewById(R.id.tv_title);
        tv_title.setText(getString(R.string.text_select));

        FrameLayout fl_close = dialog.findViewById(R.id.fl_close);
        fl_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        LinearLayoutCompat btn_ok = dialog.findViewById(R.id.btn_ok);
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        RecyclerView rv_list = dialog.findViewById(R.id.rv_list);
        if (Adapter == null) {
            Adapter = new Adapter(LastActivity.this);
        }
        Adapter.doRefresh(specialityList);
        if (rv_list.getAdapter() == null) {
            rv_list.setHasFixedSize(true);
            LinearLayoutManager layoutManager = new LinearLayoutManager(DoctorRegisterLastActivity.this);
            rv_list.setLayoutManager(layoutManager);
            rv_list.setAdapter(specialityAdapter);
        }
        dialog.show();
    }

答案 2 :(得分:0)

属性textSize适用于我:

<item name="android:textSize">18sp</item>