0

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

我什至提到了这个以前的堆栈溢出问题

这是代码:

// 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

4 回答 4

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!");
于 2014-09-08T14:30:54.197 回答
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 演示

于 2012-08-27T10:49:47.317 回答
0

尝试

dialog.getWindow().setLayout(height, width);
于 2014-04-14T17:06:05.240 回答
0
View layout = inflater.inflate(R.layout.view, NULL);
layout.setMinimumWidth(200);
layout.setMinimumHeight(200);
dialog.setContentView(layout);
于 2017-04-11T11:59:46.343 回答