2012-09-11 73 views
2

我试图做一个自定义对话框自定义对话框的宽度和高度

LayoutInflater li = LayoutInflater.from(this); 
     View view = li.inflate(R.layout.rate_layout, null); 

     RelativeLayout rate = (RelativeLayout) view.findViewById(R.id.rateClick); 
     RelativeLayout close = (RelativeLayout) view.findViewById(R.id.closeBtn); 
     close.setClickable(true); 
     rate.setClickable(true);  

     final AlertDialog.Builder dd = new AlertDialog.Builder(this);   
     dd.setView(view); 
     dd.setCancelable(false); 

     final AlertDialog d = dd.create(); 
     d.show(); 

背景图像有4种分辨率的,所以我只能做wrap content

这里是我的xml文件

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



    <RelativeLayout 
     android:id="@+id/rateClick" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:background="@drawable/rate_now_button" /> 

    <RelativeLayout 
     android:id="@+id/imageView1" 
     android:layout_width="219dp" 
     android:layout_height="234dp" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:background="@drawable/pop_up" > 

     <RelativeLayout 
      android:id="@+id/closeBtn" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentTop="true" 
      android:background="@drawable/close_button" > 
     </RelativeLayout> 

     <RelativeLayout 
     android:id="@+id/rateClick" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_alignParentBottom="true" 
     android:background="@drawable/rate_now_button" /> 

    </RelativeLayout> 

</RelativeLayout> 

这是我得到。我想如何删除对话框布局。白色的大边框。

My Image

+0

http://www.mkyong.com/android/android-custom-dialog-example/ –

+0

http://codinglookseasy.blogspot.in/2012/07/alert-box-code.html –

+0

了现在也有这个问题一段时间了。我在宽度和高度上使用'wrap_content',但也是一个很好的答案,您的问题也会对我有所帮助。 – cosmincalistru

回答

5

不要使用AlertDialog.Builder。试试这个。

Dialog d = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar); 

Dialog d = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen); 

你可以使用任何主题符合您的标准,或者创建自己的。请参阅this questionthis other question的回答。另见available themes

有关setContentView(),setCancelable(),show()和其他您可能认为有用的方法的文档,请参阅Dialog

+0

如果我使用对话框,它将图像扩展到整个屏幕....这是主要问题 –

+0

您可以随时创建一个透明的CustomDialog背景删除边界,如http://stackoverflow.com/questions/7442450/custom-alertdialog-borders –

0

尝试这样:

dialog_item = new Dialog(Pre_Venda_Digitacao_2.this); 
    dialog_item.setContentView(R.layout.layout_pre_venda_digitacao_add_produto); 
    Util.softKeyboard(dialog_item.getWindow(), false); 

    // Recuperar os componentes da janela e disponibilizar para as variáveis globais. 
    dialog_Item_getComponentes(); 

    /***********************OnShow do Dialog*****************************/ 
    dialog_item.setOnShowListener(new OnShowListener() { 
     @Override 
     public void onShow(DialogInterface dialog) { 
      //Recupera a menor fonte dentro do layout passado 
      float lower_font_size = AutoResizeTextView_Functions.Get_Lower_Font_Size(ll_add_produto, 1000); 

      //Seta menor fonte recuparada anteriormente para os textos do layout passado 
      AutoResizeTextView_Functions.Resize_Text(ll_add_produto, lower_font_size); 
     } 
    }); 

    dialog_item.setOnDismissListener(new OnDismissListener() { 

     @Override 
     public void onDismiss(DialogInterface dialog) { 
      dialog_item = null; 
      preVenda_Item = null; 
     } 
    }); 

    Display display = getWindowManager().getDefaultDisplay(); 
    int width  = display.getWidth(); 

    lp = new WindowManager.LayoutParams(); 
    lp.copyFrom(dialog_item.getWindow().getAttributes()); 
    lp.width = (int) (width - (width * 0.07)); //Tira 7% da largura total da tela para deixar um espaço na janela 
    lp.height = WindowManager.LayoutParams.WRAP_CONTENT; 

    dialog_item.getWindow().setAttributes(lp); 

    dialog_item.show(); 
4

你可以试试:

myDialog.show(); 
Window window = myDialog.getWindow(); 
window.setLayout(300, 300); 
+0

不,不要这样做,或任何硬编码宽度和高度 – Jin

+0

这工作正常。我们可以设置一个固定值WindowManager。LayoutParams.WRAP_CONTENT或屏幕宽度的百分比等。 我们需要在调用show()方法后设置此值。 如果我们需要设置背景色,我们需要在调用show()方法之前调用它。 android.view.WindowManager.LayoutParams lp = new android.view.WindowManager.LayoutParams(); lp.copyFrom(deleteDialog.getWindow()。getAttributes()); lp.dimAmount = 0.7f; deleteDialog.getWindow()。setAttributes(lp); deleteDialog.show(); –

0

为此在您的rate_layout.xml

<!-- begin snippet: js hide: false --> 
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#0000" 
> 

您还需要创建和调用对话片段如果你没有:

public class AddDialog extends DialogFragment 
{ 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) 
    { 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

     LayoutInflater inflater = getActivity().getLayoutInflater(); 

     builder.setView(inflater.inflate(R.layout.rate_layout, null)); 
     return builder.create(); 
    } 
} 

然后调用Dialog的功能:

public void SomeFunction() 
{ 
    DialogFragment d = new AddDialog(); 
    d.show(getSupportFragmentManager(), "some_dialog_refrence"); 
} 
3
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
Display display = wm.getDefaultDisplay(); 
DisplayMetrics metrics = new DisplayMetrics(); 
display.getMetrics(metrics); 
Double width = metrics.widthPixels*.7; 
Double height = metrics.heightPixels*.7; 
Window win = dialog.getWindow(); 
win.setLayout(width.intValue(), height.intValue()); 
0

我们可以设置宽度,这样的自定义对话框的高度。

DisplayMetrics displayMetrics = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); 
int screenHeight = displayMetrics.heightPixels; 
int screenWidth = displayMetrics.widthPixels; 

android.view.WindowManager.LayoutParams lp = new android.view.WindowManager.LayoutParams(); 
     lp.copyFrom(deleteDialog.getWindow().getAttributes()); 

// If we need to set background color of activity 
// We need to set this before calling show() method 
lp.dimAmount = 0.7f;  
deleteDialog.getWindow().setAttributes(lp); 
deleteDialog.show(); 

// we need to set width, height after calling show() method 
Window window = deleteDialog.getWindow(); 
window.setLayout(screenWidth - screenWidth/2), WindowManager.LayoutParams.WRAP_CONTENT); 
相关问题