簡體   English   中英

自定義對話框的寬度和高度

[英]Custom dialog box width and height

我正在嘗試創建一個自定義對話框

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>

這就是我得到的。 我怎么想刪除對話框布局。 白色的大邊界。

我的形象

不要使用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);

您可以使用符合條件的任何主題,也可以創建自己的主題。 請參閱此問題的答案和其他問題 另請參閱可用主題

有關setContentView(),setCancelable(),show()以及您可能認為有用的其他方法的文檔,請參閱Dialog

你可以試試:

myDialog.show();
Window window = myDialog.getWindow();
window.setLayout(300, 300);
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());

嘗試這樣的事情:

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

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

如果你沒有,你還需要創建並調用一個對話框Fragment:

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

我們可以像這樣設置自定義對話框的寬度,高度。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM