Android LayoutInflater无效

时间:2013-09-20 10:39:25

标签: android xml layout-inflater

我创建了一个xml文件(一个对话框),我想用LayoutInflater减少它,但它总是像我的屏幕一样大。这是代码:

protected Dialog onCreateDialog(int id) {
     switch(id) {
     case NICKNAME_DIALOG_ID:
         LayoutInflater inflater = (LayoutInflater) 
                 getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             final View layout = inflater.inflate(R.layout.nickname_dialog, 
                 (ViewGroup) findViewById(R.id.linearLayout_root));
             final EditText nickname =   
                (EditText)findViewById(R.id.editText_nickname);   

            AlertDialog.Builder nicknameBuilder = new AlertDialog.Builder(this);
            nicknameBuilder.setView(layout);
            nicknameBuilder.setTitle(R.string.nicname_dialog_title);

            nicknameBuilder.setNegativeButton(R.string.nickname_dialog_cancel, 
                new DialogInterface.OnClickListener() {
                @SuppressWarnings("deprecation")
        public void onClick(DialogInterface dialog, int whichButton) {                      
                    SettingsActivity.this.removeDialog(NICKNAME_DIALOG_ID);
                }
            });

            nicknameBuilder.setPositiveButton(R.string.nickname_dialog_ok, 
                new DialogInterface.OnClickListener() {
                @SuppressWarnings("deprecation")
            public void onClick(DialogInterface dialog, int which) {
                    String text = nickname.getText().toString();
                    Editor editor = m_GameSettings.edit();
                    editor.putString(VolimHrvatsku.GAME_PREFERENCES_NICKNAME,
                            text);
                    editor.commit();
                    SettingsActivity.this.removeDialog(NICKNAME_DIALOG_ID);
                }
            });
            AlertDialog nicknameDialog = nicknameBuilder.create();
            return nicknameDialog;

     }

    return null;

}

昵称对话框xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/sahovnica"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView_enter_nickname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/settings_enter_nick"
    android:textColor="@color/dark_grey"
    android:textSize="@dimen/scores_text_dim"
    android:textStyle="bold" />

<EditText
    android:id="@+id/editText_nickname"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" >

    <requestFocus />
</EditText>

<RelativeLayout
    android:id="@+id/relativeLayout_nickname"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

</RelativeLayout>

</LinearLayout>

电子邮件对话框xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout_email"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/sahovnica"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView_body"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/email_dialog_text"
    android:textColor="@color/dark_grey"
    android:textSize="@dimen/scores_text_dim"
    android:textStyle="bold" />

<EditText
    android:id="@+id/editText_body"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:ems="10" >

    <requestFocus />
</EditText>

<RelativeLayout
    android:id="@+id/relativeLayout_email"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

</RelativeLayout>

</LinearLayout>

我想问btw,是否可以改变AlertDialog中YES和NO按钮的外观? 提前谢谢......

1 个答案:

答案 0 :(得分:0)

emaildialog.xml中执行布局宽度和高度wrap_content

我想是的,这可能对你有帮助。