如何在警报对话框中将标题和正/负按钮设置为黑色

问题描述 投票:2回答:4

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

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

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

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

java android xml alertdialog
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>

Source

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

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

Source

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


1
投票

您可以像doydoy那样定义自己的AlertDialog样式,然后以这种方式创建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


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

-1
投票

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

RES /值/ 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"/>
© www.soinside.com 2019 - 2024. All rights reserved.