eclipse 自定义对话框宽高

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12365736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 18:57:27  来源:igfitidea点击:

Custom dialog box width and height

javaandroideclipseandroid-layout

提问by Muhammad Umar

I am trying to make a custom dialog box

我正在尝试制作自定义对话框

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();

The background image has 4 types of resolutions so I can do wrap contentonly.

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

Here is my xml file

这是我的 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>

This is what I am getting. How am i suppose to remove the dialog layout. The white big borders.

这就是我得到的。我怎么想删除对话框布局。白色的大边框。

My Image

我的形象

回答by Andy Harris

Don't use AlertDialog.Builder. Try this instead.

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

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

or

或者

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

You can use whatever theme meets your criteria, or create your own. See answer to this questionand this other question. See also available themes.

您可以使用任何符合您标准的主题,也可以创建您自己的主题。请参阅此问题此其他问题的答案。另请参阅可用主题

See Dialogfor documentation on setContentView(), setCancelable(), show() and other methods you might find useful.

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

回答by Steven.Nguyen

You can try:

你可以试试:

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

回答by Ankit Adlakha

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());

回答by Rukmal Dias

we can set width, height of custom dialog like this.

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

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);

回答by Toye_Brainz

Do this in your rate_layout.xml:

在您的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"
>

You also need to create and call a dialog Fragment if you didnt:

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

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();
    }
}

Then call the Dialogin a function:

然后Dialog在函数中调用:

public void SomeFunction()
{
    DialogFragment d = new AddDialog();
    d.show(getSupportFragmentManager(), "some_dialog_refrence");
}

回答by William Da Silva

Try something like this:

尝试这样的事情:

    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();