自定义警报对话框未显示

spr*_*lls 1 android android-alertdialog

我想显示一个自定义警报对话框,其结构已在XML文件中定义.这是我使用的代码.

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(findViewById(R.layout.inputform));
builder.show();
Run Code Online (Sandbox Code Playgroud)

单击按钮时会触发它.但是,窗口没有显示出来.我在屏幕顶部有一个透明的灰色层,无法点击任何东西.但是,使用setMessage或显示复选框等一些基本代码效果很好.任何人都可以指出我做错了吗?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent">

<TableLayout android:id="@+id/TableLayout01"
    android:layout_width="fill_parent" android:layout_height="fill_parent">

<TableRow android:id="@+id/TableRow01" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:text="Room name" android:id="@+id/TextView01"
        android:layout_width="fill_parent" android:layout_height="wrap_content"/>
    <EditText android:hint="Enter room name" android:id="@+id/EditText01"
        android:layout_width="0dp" android:layout_height="wrap_content"
        android:layout_weight="1"/>
</TableRow>

<TableRow android:id="@+id/TableRow02" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:text="Point id" android:id="@+id/TextView02"
        android:layout_width="fill_parent" android:layout_height="wrap_content"/>
    <EditText android:hint="Enter point id" android:id="@+id/EditText02"
        android:layout_width="0dp" android:layout_height="wrap_content"
        android:layout_weight="1"/>
</TableRow>

<TableRow android:id="@+id/TableRow03" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button android:text="OK" android:id="@+id/btnOK"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</TableRow>

</TableLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

mai*_*450 6

这是因为findViewById用于在当前活动的布局中查找特定视图,而不是用于加载整个布局.

您可以使用@Matt 答案,也可以使用如下的LayoutInflater来扩展布局:

LayoutInflater li = getLayoutInflater();
View v = li.inflate(R.layout.inputform);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(v);
builder.show();
Run Code Online (Sandbox Code Playgroud)

(没试过,但我认为它应该工作).