2014-05-07 82 views
0

我正在使用android中的自定义alertDialog,并发现一个问题。我的代码很简单:android中的自定义alertDialog

LayoutInflater inflatre = getLayoutInflater(); 
View v = inflatre.inflate(R.layout.dialog_view, null); 
AlertDialog.Builder dialog = new AlertDialog.Builder(EditPageActivity.this); 
dialog.setView(v); 
dialog.show(); 

而且布局XML是:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/bg" 
android:padding="10dp" 
android:gravity="center" > 

<Button 
    android:id="@+id/gallery_b" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Gallery" /> 

<Button 
    android:id="@+id/photo_b" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/gallery_b" 
    android:text="Take Photo" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/gallery_b" 
    android:layout_centerHorizontal="true" 
    android:text="Choose Image" 
    android:textSize="20sp" 
    android:textColor="@color/splash_font" 
    android:layout_marginBottom="10dp" /> 

但输出是这样的:

enter image description here

感觉谷歌搜索后无奈:(......任何人都可以形容我的任何东西

+0

集'的android:layout_width = “FILL_PARENT” 机器人:layout_height = “FILL_PARENT”'到主布局,并尝试 –

+0

删除填充或使用对话背景图像的高度较大或只是手动设置布局的高度 –

回答

1

试试这个布局Dialog,它应该工作。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/bg" 
    android:padding="10dp" 
    android:gravity="center"> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:text="Choose Image" 
     android:textSize="20sp" 
     android:textColor="@color/splash_font"/> 

    <Button 
     android:id="@+id/gallery_b" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView1" 
     android:text="Gallery" 
     android:layout_marginTop="10dp"/> 

    <Button 
     android:id="@+id/photo_b" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/gallery_b" 
     android:text="Take Photo" /> 

</RelativeLayout> 

enter image description here

+0

以及...感谢兄弟,你的布局为我工作....你能请描述为什么我的布局没有工作的事实像你的......我的布局几乎相似你的虽然... –

+0

@MostafaImran注意这些对齐参数。 ''' ' –

1

试试这个

Dialog dialog = new Dialog(YourActivity.this); 
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
        dialog.setContentView(R.layout.yourlayout); // your layout id 
        Button dialogButton = (Button) dialog 
          .findViewById(R.id.dialogButtonOK); // button in your layout 
        // if button is clicked, close the custom dialog 
        dialogButton.setOnClickListener(new OnClickListener() { 

         @Override 
         public void onClick(View v) { 


         } 
        }); 

        dialog.show(); 
1

//试试这个:

 final Dialog dialog = new Dialog(mContext); 
     dialog.requestWindowFeature((int) Window.FEATURE_NO_TITLE); 
     dialog.setContentView(R.layout.your_layout_here); 
     WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); 
     lp.copyFrom(dialog.getWindow().getAttributes()); 
     lp.width = WindowManager.LayoutParams.WRAP_CONTENT; 
     lp.height = WindowManager.LayoutParams.WRAP_CONTENT; 
     dialog.getWindow().setAttributes(lp); 
     dialog.show();