[英]Android:How can I set the AlertDialog width and height,and the button of the AlertDialog style?
[英]Why can Android AlertDialog's width and height = -1
我對AlertDialog有三個問題。 首先,我將發布我的代碼,然后問我的問題。
<LinearLayout
android:id="@+id/l2"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="@android:color/holo_orange_light"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="200dp"
android:layout_height="600dp"
android:text="Test Test"
android:textSize="40sp"
android:background="@android:color/holo_green_light"
android:textColor="@android:color/black"/>
這是要顯示在AlertDialog中的視圖,在activity_main.xml中,我設置了一個按鈕來打開AlertDialog,以下是偵聽器的代碼。
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.popup_window, null);
AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).setView(view).create();
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
Log.d("TAG", "First lp width and height" + lp.width + " " + lp.height);
dialog.show();
Window dialogWindow = dialog.getWindow();
dialogWindow.setLayout(500,500);
WindowManager.LayoutParams lp2 = dialogWindow.getAttributes();
Log.d("TAG", "lp2 width and height" + lp2.width + " " + lp2.height);
}
});
下面的logcat信息屏幕快照: 日志問題:
dialogWindow.setLayout(500,500);
在dialog.show
之后執行。當我放置dialogWindow.setLayout(500,500);
在dialog.show
前面,我發現它不起作用。 為什么 getWindow.setLayout(int width, int height)
; 寬度和高度的單位是多少? 屏幕截圖 1和2
在您調用.show()
或.create()
之前,不會創建.show()
.create()
。 因此,如果在創建之前調用要調用的方法,它將僅返回一些默認值。
如果你這樣做
AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).setView(view).create();
dialog.create();
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
Log.d("TAG", "First lp width and height" + lp.width + " " + lp.height);
它應該顯示正確的值。
3
與您在xml中輸入的值相同。 可以是MATCH_PARENT
, WRAP_CONTENT
或精確值。 精確值以像素為單位。
另請參閱Window的文檔
如您所見,Dialog的寬度和高度等於-1,我不知道原因。
-1
是WindowManager.LayoutParams.width
和WindowManager.LayoutParams.height
的默認值。
-1
也稱為MATCH_PARENT
,這意味着對話框將在尊重主題屬性(例如最大和最小寬度和高度)的同時嘗試占用盡可能多的空間。
在
dialog.show()
之后執行dialogWindow.setLayout(500,500)
dialog.show()
。 當我將它放在dialog.show()
前面時,它不起作用。 為什么?
dialog.show()
在dialog.show()
或dialog.create()
之前安裝其內容。 它在內部調用以下PhoneWindow
方法:
@Override
public void setContentView(View view) {
setContentView(view, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
}
這也應解釋默認值的來源。 您對setLayout
調用現在應該會產生所需的結果。
信用: @TimCastelijns
getWindow.setLayout(int width, int height)
寬度和高度的單位是什么?
單位是像素。 您還可以使用WRAP_CONTENT
和MATCH_PARENT
常量。
如果要使用此方法,請避免對像素進行硬編碼,因為在不同的屏幕上您會得到不同的視覺效果。 這些方法將幫助您:
/** Precise decimal value, use for translation. */
public static float dpToPx(Context context, int dp) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
}
/** Rounded down value, use for offsets, paddings, margins. */
public static int dpToPxOffset(Context context, int dp) {
return (int) (dpToPx(context, dp));
}
/** Rounded up value, use for sizes. */
public static int dpToPxSize(Context context, int dp) {
return (int) (0.5f + dpToPx(context, dp));
}
以下各行將被忽略。
android:layout_width="200dp"
android:layout_height="200dp"
在由父視圖提供的LayoutParams
對象上設置layout_
前綴的屬性。 由於沒有父級,因此沒有可以使用的LayoutParams
。 嘗試改為設置android:minHeight
和android:minWidth
。
根元素的父視圖由膨脹參數決定:
LayoutInflater.from(context).inflate(R.layout.popup_window, /*parent*/ null);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.popup_window, null);
AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).setView(view).create();
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
Log.d("TAG", "First lp width and height" + lp.width + " " + lp.height); <---------
dialog.show(); <----------- you show the window here, so its size can be calculated. Before this, it doesn't exist. This is why you get -1 -1
Window dialogWindow = dialog.getWindow();
dialogWindow.setLayout(500,500);
WindowManager.LayoutParams lp2 = dialogWindow.getAttributes();
Log.d("TAG", "lp2 width and height" + lp2.width + " " + lp2.height);
}
});
在您的第一個日志之前嘗試使用dialog.show;)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.