Android - 为自定义对话框指定自定义主题:AlertDialog.Builder将内容包装在对话框中。

4

我创建了一个继承自'Theme.Holo.Light.Dialog'的自定义主题。

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

我的代码:

private AlertDialog testDialog;
AlertDialog.Builder testBuilder;
LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.test_dialog,
                                   (ViewGroup) findViewById(R.id.test_root));
testBuilder = new AlertDialog.Builder(this, R.style.cust_dialog);
testBuilder.setView(layout);
testBuilder.setTitle("Support");
testDialog = testBuilder.create();
testDialog.show();

这会导致我的对话框出现在另一个对话框中。如何解决这个问题?
谢谢。
编辑:这是我的test_dialog.xml布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/test_root"
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          >


        <Button
            android:id="@+id/test1"
            android:layout_width="300dp"
            android:layout_height="75dp" 
            android:text="@string/test"

            android:gravity="center" />

        <Button
            android:id="@+id/test2"
            android:layout_width="300dp"
            android:layout_height="75dp" 
            android:text="@string/test"
            android:layout_below="@id/test1"
            android:gravity="center" />

</RelativeLayout>

1
也许你的 R.layout.test_dialog 包含的不仅仅是内容?setView(): "设置自定义视图作为对话框的内容"。如果你在那里添加了一个完整的对话框布局,你最终会得到一个对话框内部的对话框。 - zapl
我已经添加了我的test_dialog布局xml。里面什么都没有,只有两个按钮。 - CLDev
应该是由你在充气时指定的根元素引起的。尝试使用View layout = inflater.inflate(R.layout.test_dialog,null);,否则你会将布局充气到自身中。 - zapl
不行,这样不行。仍然在对话框中显示一个对话框。 - CLDev
3个回答

7

试着尝试这个。

ContextThemeWrapper ctw = new ContextThemeWrapper( this, R.style.MyTheme );
            AlertDialog.Builder builder= new AlertDialog.Builder( ctw );
            LayoutInflater inflater = (LayoutInflater) ctw.getSystemService(LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.customdialogue,
                                           (ViewGroup) findViewById(R.id.layout_root));

1
ContextThemeWrapper ctw = new ContextThemeWrapper( this, R.style.MyTheme ); AlertDialog.Builder builder= new AlertDialog.Builder( ctw ); 这个方法解决了我的问题。 - howettl

0
你可以试试这个。
AlertDialog.Builder builder = new AlertDialog.Builder(activity, R.layout.test_dialog);

0

通过上下文包装器的上下文获取布局的Inflater非常重要,而不是通过通常的上下文...在那一点上也有些困难。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接