0

这是我的对话框代码

     AlertDialog.Builder builderofme = new AlertDialog.Builder(this);

        final FrameLayout frameView = new FrameLayout(this);
        builderofme.setView(frameView);

       builderofme.setCancelable(true);



        builderofme.setPositiveButton(" Yes ", new OkOnClickListener());
        builderofme.setNegativeButton(" No ", new CancelOnClickListener());
        final AlertDialog alertDialog = builderofme.create();
        LayoutInflater inflater = alertDialog.getLayoutInflater();
        View dialoglayout = inflater.inflate(R.layout.myediteddialog, frameView);
        alertDialog.show();

代码运行良好。但我想将白色背景用于“是”和“否”按钮。现在它就像这样......

在此处输入图像描述

这是我的 myediteddialog.xml 文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="vertical"
android:background="@drawable/delete"
 >

<TextView
    android:id="@+id/editeddialogboxtextview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@android:color/background_dark"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Do You Want To Delete?"
    android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>
4

6 回答 6

1

根据您的要求,您必须设计自定义对话框布局,此布局包含一个文本视图和 2 个按钮,它们看起来像默认警报对话框。并根据您的要求为两个按钮编写侦听器并设计按钮。

于 2013-09-24T10:51:23.897 回答
0

你可以看下一个例子

创建自定义布局

如果您想在对话框中自定义布局,请创建一个布局并通过在 AlertDialog.Builder 对象上调用 setView() 将其添加到 AlertDialog。

默认情况下,自定义布局填充对话框窗口,但您仍然可以使用 AlertDialog.Builder 方法添加按钮和标题。

于 2013-09-24T11:07:25.577 回答
0

使用自定义对话框,这是最好的例子

自定义对话框

于 2013-09-24T11:41:31.877 回答
0

这里可以在布局文件中声明这两个按钮myediteddialog.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="vertical"
android:background="@drawable/delete"
 >

<TextView
    android:id="@+id/editeddialogboxtextview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@android:color/background_dark"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Do You Want To Delete?"
    android:textAppearance="?android:attr/textAppearanceMedium" />


<Button
        android:id="@+id/stopButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="ffffff"
       android:text="Yes" />

    <Button
        android:id="@+id/playButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:background="ffffff"
        android:text="No" />

</RelativeLayout>
于 2013-09-24T11:13:56.027 回答
0

我使用很少的 xml 以我的方式完成了此操作,并且没有更改任何其他内容。想与寻求解决方案的你们分享。

首先,我为我的按钮创建了一个自定义选择器可绘制 xml 文件

这里是

  <selector xmlns:android="http://schemas.android.com/apk/res/android" >
 <item android:state_pressed="true" >
     <shape android:shape="rectangle"  >
         <corners android:radius="3dip" />
         <stroke android:width="1dip" android:color="#83776b" />
         <gradient  android:angle="-90"  android:startColor="#5f4427" android:endColor="#cbb59d"  />           
     </shape>
 </item>
<item android:state_focused="true">
     <shape android:shape="rectangle"  >
         <corners android:radius="3dip" />
         <stroke android:width="1dip" android:color="#83776b" />
         <solid  android:color="#92806c"/>      
     </shape>
 </item> 
<item >
    <shape android:shape="rectangle"  >
         <corners android:radius="3dip" />
         <stroke android:width="1dip" android:color="#83776b" />
         <gradient  android:angle="-90"  android:startColor="#cbb59d"    android:endColor="#92806c" />           
     </shape>
   </item>
</selector>

然后在我的活动中,我包含了这两行

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

 Button c = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
        if(c != null)
                   b.setBackgroundDrawable(getResources().getDrawable(R.drawable.dialogbutton));

对我来说很简单的解决方案。

于 2013-09-24T11:36:37.650 回答
0

目前您正在使用默认主题,这是因为您没有获得背景颜色,因为Dark holo theme如果您希望所有设备上的主题一致,那么您应该在 xml 文件本身中声明按钮,然后相应地使用它。根据我的经验,这将是最好和正确的方法。

于 2013-09-24T10:45:17.477 回答