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

时间:2012-02-16 15:40:56

标签: android dialog alertdialog

我创建了一个继承自'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();

这会导致我的对话框在对话框中。我该如何解决这个问题?

感谢。

EDIT ::::

这是我的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>

3 个答案:

答案 0 :(得分:6)

试试这个。

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 :(得分:0)

不要通过usuall上下文来获取布局的Inflater是非常重要的,但是通过上下文包装器的上下文......在这一点上也有点了解

答案 2 :(得分:0)

你可以试试这个

AlertDialog.Builder builder = new AlertDialog.Builder(activity, R.layout.test_dialog);