2

我当前的警报对话框看起来像右边的那个,我希望它看起来像左边的那个。

在此处输入图像描述

我想我可以通过更改自定义对话框的主题来做到这一点,但我不确定这是否正确,如果是这样,那么如何进行?无论如何我可以自定义更改正/负按钮吗?

我不想创建我的自定义对话框,因为按钮在警报对话框中显示的方式对我来说是完美的。

类似于http://developer.android.com/guide/topics/ui/dialogs.html中右侧的内容

这是我到目前为止所做的,

    AlertDialog.Builder builder = new AlertDialog.Builder(LoginSignupActivity.this);
    LayoutInflater inflater = getLayoutInflater();  
    builder.setView(inflater.inflate(R.layout.newusersignup, null)).setTitle("Sign up");
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {}
    //etc

我的xml看起来像

<TextView
    android:id="@+id/newUsersTxtName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Enter your name"
    android:textColor="@color/white"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/newUserTxtPassword"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/newUsersName"
    android:text="Enter password"
    android:textColor="@color/white" />

<EditText
    android:id="@+id/newUsersName"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/newUsersTxtName"
    android:ems="10"
    android:textColor="@color/white"
    android:inputType="text" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/newUserPassword"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/newUserTxtPassword"
    android:ems="10"
    android:textColor="@color/white"
    android:inputType="textPassword" />

<EditText
    android:id="@+id/newUserEmail"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/newUserTxtEmail"
    android:ems="10"
    android:textColor="@color/white"
    android:inputType="textEmailAddress" />

<TextView
    android:id="@+id/newUserTxtEmail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/newUserPassword"
    android:text="Enter your email"
    android:textColor="@color/white"
    android:textAppearance="?android:attr/textAppearanceSmall" />

4

4 回答 4

2

我会通过“styles.xml”中的自定义样式进行更改,例如

    <?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AlertDialogCustom" parent="@android:style/AlertDialog">
    <item name="android:textColor">#00FF00</item>
    <item name="android:typeface">monospace</item>
    <item name="android:textSize">10sp</item>
</style>

资源

也可以以编程方式执行此操作:

AlertDialog.Builder customBuilder = new AlertDialog.Builder(new ContextThemeWrapper(this,android.R.style.Theme_Dialog));

customBuilder.setTitle(R.string.popup_error_title);
customBuilder.setNegativeButton("Exit application", new DialogInterface.OnClickListener() {  
                public void onClick(DialogInterface dialog, int which) {  
                    MyActivity.this.finish();
                }  
        });
AlertDialog dialog = customBuilder.create();
    dialog.show();

Button b = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
if(b != null)
        b.setBackgroundDrawable(getResources().getDrawable(R.drawable.my_button))

资源

编辑:styles.xml 文件将放置在 \main\res\values 文件夹中

于 2013-10-21T15:17:49.393 回答
1

AlertDialog你可以像 doydoy 那样定义自己的风格,然后简单地AlertDialog.Builder以这种方式创建:

AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.style_name);

否则,您可以尝试定义自己的应用程序主题并android:alertDialogStyle在不接触代码的情况下进行覆盖,以获得统一的用户体验。https://sites.google.com/site/androidhowto/how-to-1/customize-alertdialog-theme

于 2013-10-21T16:22:47.960 回答
0

使用此方法,您可以更改尺寸颜色和其他一些功能

dialog = alertdialogbuilder.create();

        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dlg) {
                dialog.getButton(Dialog.BUTTON_POSITIVE).setTextSize(20); // set text size of positive button
                dialog.getButton(Dialog.BUTTON_POSITIVE).setTextColor(Color.RED); set text color of positive button

                dialog.getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL); // set title and message direction to RTL
            }
        });
        dialog.show();
于 2018-02-27T17:11:38.490 回答
-1

这对我有用,它基本上将 Holo 主题应用于我的特定活动

res/values/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
        <style name="default_activity_theme" parent="@android:style/Theme.Holo"/>
</resources>

AndroidManifest.xml

<activity android:name="MyActivity"
          android:theme="@style/default_activity_theme"/>
于 2013-10-22T15:51:35.057 回答