Android 如何设置对话框按钮白色图像?

Android 如何设置对话框按钮白色图像?,android,layout,dialog,Android,Layout,Dialog,这是我的对话代码 AlertDialog.Builder builderofme = new AlertDialog.Builder(this); final FrameLayout frameView = new FrameLayout(this); builderofme.setView(frameView); builderofme.setCancelable(true); builderofme.setPo

这是我的对话代码

     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>


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

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

您可以看到下一个按钮

创建自定义布局

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


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

此处您可以在
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>

我用自己的方式做到了这一点,只使用了很少的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>

对我来说,这是一个简单的解决方案。使用自定义对话框,这里是最好的例子


我正在使用sherlock主题。它在我的活动中。如何在对话框中使用不同的主题?我在xml中使用了不同的主题。不知怎的,它没有出现。这也是从默认对话框继承的按钮。我使用的是alert对话框,而不是默认对话框。必须使用AlertDialog吗?如果不是,我建议创建自己的对话框并放置两个按钮,这样会更加健壮和可控。希望我帮助你或提供一些指导。
 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));