在对话框中设置高度和宽度?

时间:2012-08-27 10:38:20

标签: android android-layout android-dialog dialog

我正在使用XML布局,我提示它作为对话框。 XML布局的设计格式很好,具有足够的所需高度和宽度。 但是当我打开它作为对话框时,它的宽度会受到干扰,所以如何通过编码来设置对话框的高度和宽度。

我甚至提到了之前的STACK OVERFLOW QUESTION

以下是代码:

// Layout Inflater Code..
    editDialog = new Dialog(this);
        layoutEdit = LayoutInflater.from(this).inflate(R.layout.createlayout, null);

    //layoutEdit.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
    editDialog.setContentView(layoutEdit);

// Called the Dialogbox to inflate

updateButton.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                editDialog.show();  
                }
            });

// XML文件代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >



<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/bd"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:freezesText="false"
        android:text="Enter Name"

        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/whtie"
        android:typeface="monospace" />


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

    </EditText>


</LinearLayout>    
</ScrollView>

感谢您的帮助。

此致

4 个答案:

答案 0 :(得分:1)

试试这个......

1. 对话框摘录:

private void CustomDialog(String msg) {
    final Dialog dialog = new Dialog(YourActivity.this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    LinearLayout.LayoutParams dialogParams = new LinearLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, 300);//set height(300) and width(match_parent) here, ie (width,height)

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View dislogView = inflater
            .inflate(R.layout.my_custom_popup, null);

    dialog.setContentView(dislogView, dialogParams);
    TextView popupMsg = (TextView) dialog.findViewById(R.id.popupMsg);
    Button popupOk = (Button) dialog.findViewById(R.id.popupOk);
    popupMsg.setText(msg);
    popupOk.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    dialog.show();
}

2.然后调用CustomDialog(Str),在活动中提示。

CustomDialog("This is customized popup dialog!");

3.快乐编码......

答案 1 :(得分:0)

你最好使用一个看起来像对话框的活动(我觉得在你的情况下会更好)这里是一个示例代码:

    public class DialogActivity extends Activity {
/**
 * Initialization of the Activity after it is first created.  Must at least
 * call {@link android.app.Activity#setContentView setContentView()} to
 * describe what is to be displayed in the screen.
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    // Be sure to call the super class.
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_LEFT_ICON);

    // See assets/res/any/layout/dialog_activity.xml for this
    // view layout definition, which is being set here as
    // the content of our screen.
    setContentView(R.layout.dialog_activity);

    getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, 
            android.R.drawable.ic_dialog_alert);
}
}

此代码来自api demos

答案 2 :(得分:0)

dialog.getWindow()。setLayout(height,width);

答案 3 :(得分:0)

View layout = inflater.inflate(R.layout.view, NULL);
layout.setMinimumWidth(200);
layout.setMinimumHeight(200);
dialog.setContentView(layout);