【问题标题】:AlertDialog action button placementAlertDialog 操作按钮放置
【发布时间】:2015-12-31 07:46:20
【问题描述】:

我使用AlertDialog.Builder 创建了一个AlertDialog,并希望调整操作按钮(取消和确定)的位置以改善其外观。

我已经搜索过了,似乎找不到任何简单的方法。我是否必须放弃AlertDialog.Builder 并将操作按钮功能添加到我的视图并设置所有侦听器?如果是这样,我如何确定操作按钮的文本大小/颜色以与其他 AlertDialog 保持一致?

popup_tee_selection_edittext.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >

<TextView
    android:id="@+id/popup_tee_selection_TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/enter_new_tee"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/popup_tee_selection_EditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <requestFocus />
</EditText>

<Switch
    android:id="@+id/popup_tee_selection_Switch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal|start"
    android:showText="true"
    android:textOff="Men"
    android:textOn="Women"
    android:thumbTextPadding="10dp"
    android:thumb="@drawable/custom_switch_inner_holo_light"
    android:track="@drawable/custom_switch_track_holo_light"/>

</LinearLayout>

代码片段

private void popupTeeSelectionEditText() {
    View popupView = LayoutInflater.from(getActivity()).inflate(R.layout.popup_tee_selection_edittext, null);
    final EditText userInput = (EditText) popupView.findViewById(R.id.popup_tee_selection_EditText);

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
    alertDialogBuilder
            .setView(popupView)
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    String selectedItem = userInput.getText().toString();
                    if (DEBUG) Log.w(TAG, "Returned value: '" + selectedItem + "'");
                    if (listViewAlertDialog != null)
                        listViewAlertDialog.cancel();
                    processTeeSelectionCourse(selectedItem);
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            })
            .show();
}

【问题讨论】:

  • 嗨。您正在寻找的最终输出到底是什么?与“男人”开关对齐的按钮?
  • 没错。在我的屏幕截图中,蓝线表示我喜欢对话框底部的位置,并且操作按钮将与开关对齐。

标签: android android-alertdialog


【解决方案1】:

AFAIK,提供给对话框构建器的视图仅适用于对话框的中心部分,顶部没有标题部分,底部没有按钮部分。要获得您想要的外观,您需要将自定义按钮添加到此视图,设置适当的侦听器,然后隐藏原始按钮。

要设置要匹配的颜色,请检查应用的主色和强调色是什么。如果没有指定,您可以将它们设置为 android 默认值,或者可能在应用主题设置中指定它们,以便应用中的所有对话框都具有相同的外观。

查看answer,了解如何为对话框按钮设置自定义颜色。

【讨论】:

  • 感谢您确认我的怀疑。是时候自定义整个对话框了!
【解决方案2】:

AlertDialog 方法getButton 可用于在创建对话框后获取操作按钮。然后,您可以根据需要更改 textSize、textColor 等。我不认为你将能够改变它的位置。

要相对于您的其他内容视图精确定位按钮,您需要自己在popupView 中添加它们。

【讨论】:

    【解决方案3】:

    你搜索过其他类似的堆栈溢出问题吗?

    这里有一些解决方案可能对您有所帮助。链接如下。

    对于第二个链接,一直向下滚动到底部,他们简要讨论了有关创建您自己的自定义对话框的内容。使用自定义对话框,您应该能够获得所需的格式。

    Sound Conception提到的getButton()方法也可能是一个可行的方法。

    【讨论】:

    • 是的,正如我在最初的问题中所说,我已经搜索但找不到简单的方法。感谢您的链接。
    • @DaveFite,没问题!很高兴看到您的问题已得到解决 =)。
    猜你喜欢
    • 2021-07-15
    • 1970-01-01
    • 1970-01-01
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多