自定义对话框只显示一个小白框

时间:2015-10-08 13:23:57

标签: android layout dialog

我正在尝试显示自定义对话框。我复制了一个完美的类,并将它用于此对话框,但除了屏幕中间的小盒子外,它不会显示任何内容。

我无法弄清楚出了什么问题......

Dialog onCreate:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        final View v = inflater.inflate(R.layout.fleet_select_dialog, null);

        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        int width = (display.getWidth() );
        int height = (display.getHeight() );
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,(width/3)*2 );            
    }

xml layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:id="@+id/fleet_select_dialog">

    <EditText
        android:drawableLeft="@android:drawable/ic_menu_search"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:id="@+id/edit_fence2"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:layout_marginTop="15dip"
        android:textColor="@color/themeapp"
        android:background="@drawable/layout_corner_white"
        android:singleLine="true"
        android:inputType="textCapWords"/>

    <ExpandableListView
        android:layout_below="@+id/edit_fence2"
        android:layout_marginTop="5dip"
        android:id="@+id/lvExp"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:cacheColorHint="#00000000"
        android:divider="@color/themeapp"
        android:dividerHeight="1dp"
        android:layout_marginBottom="30dip"
        android:layout_above="@+id/footerview" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="40dip"
        android:id="@+id/footerview"
        android:layout_alignParentBottom="true">

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Confirm"
            android:paddingBottom="10dip"
            android:id="@+id/button_view"
            android:textColor="@color/themeapp"
            android:textSize="24dip"
            android:background="#ffffffff"
            android:paddingLeft="20dip"
            android:paddingRight="8dip"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"/>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:id="@+id/waiting"
        android:layout_height="fill_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:text="Loading..."
            android:id="@+id/textView16" />
    </RelativeLayout>
</RelativeLayout>

对话框的使用方式如下:

fleetSelectDialog = new FleetSelectDialog(context);
                fleetSelectDialog.show();

我做错了什么?

4 个答案:

答案 0 :(得分:0)

在使用inflater设置内容视图之前,请尝试设置对话框的windowLayout。

答案 1 :(得分:0)

您可以直接从Layout Inflater创建视图,只需使用布局XML文件的名称和文件中布局的ID。

使用以下代码设置AlertDialog的布局

LayoutInflater inflater = getLayoutInflater(); View dialoglayout = inflater.inflate(R.layout. fleet_select_dialog, null); 
AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setView(dialoglayout);
builder.show();

答案 2 :(得分:0)

我意外删除了一条重要的一行:

this.setContentView(v);

答案 3 :(得分:0)

                            final Dialog dialog = new Dialog(mContext);                             
                            // hide to default title for Dialog    

                            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

                                // inflate the layout dialog_layout.xml and set it
                                // as contentView
                                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                                View view = inflater.inflate(R.layout.customize_dialog, null, false);
                                dialog.setCanceledOnTouchOutside(false);
                                dialog.setContentView(view);
                                dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
                                dialog.show();

享受完整的代码。

干杯!