![](/img/trans.png)
[英]Unable to maintain the state of a custom dialog button when screen orientation changes
[英]Change the dimension of a custom dialog when orientation changes
方向更改時如何在android中更改Dialog的尺寸。
onCreate中的代碼
static final float[] DIMENSIONS_DIFF_LANDSCAPE = {20, 60};
static final float[] DIMENSIONS_DIFF_PORTRAIT = {40, 60};
final float scale = getContext().getResources().getDisplayMetrics().density;
int orientation = getContext().getResources().getConfiguration().orientation;
float[] dimensions =
(orientation == Configuration.ORIENTATION_LANDSCAPE)
? DIMENSIONS_DIFF_LANDSCAPE : DIMENSIONS_DIFF_PORTRAIT;
addContentView(mContent, new LinearLayout.LayoutParams(
display.getWidth() - ((int) (dimensions[0] * scale + 0.5f)),
display.getHeight() - ((int) (dimensions[1] * scale + 0.5f))));
當方向改變時,我想做同樣的事情。 請幫忙
只需檢查一下它是一個自定義代碼即可根據Oretbtation設置對話框高度:它通過實現此功能對我有用,使對話框變得靈活...在平板電腦中使用此功能即可。
WindowManager winMan = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
int height = winMan.getDefaultDisplay().getHeight();
int width=winMan.getDefaultDisplay().getWidth();
if(height>width){
if(DeviceType.getDeviceType(context)==Device.NEXUS7){
dialoglayout.setLayoutParams(new FrameLayout.LayoutParams(700,1100));
}else if (DeviceType.getDeviceType(context)==Device.GALAXYTAB2){
dialoglayout.setLayoutParams(new FrameLayout.LayoutParams(580,900));
}else if(DeviceType.getDeviceType(context)==Device.GALAXY10){
dialoglayout.setLayoutParams(new FrameLayout.LayoutParams(700,1100));
}
else{
if(DeviceType.getDeviceType(context)==Device.NEXUS7){
dialoglayout.setLayoutParams(new FrameLayout.LayoutParams(700,700));
}else if (DeviceType.getDeviceType(context)==Device.GALAXYTAB2){
dialoglayout.setLayoutParams(new FrameLayout.LayoutParams(700,500));
System.out.println("galaxy tab 2 landescape");
}else if(DeviceType.getDeviceType(context)==Device.GALAXY10){
dialoglayout.setLayoutParams(new FrameLayout.LayoutParams(700,700));
}
我建議您創建一個方法init,然后將要發生的事情放在onCreate和方向更改中。
首先,您應該將此添加到您的活動xml中
android:configChanges="keyboardHidden|orientation"
然后實施方法
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
static final float[] DIMENSIONS_DIFF_LANDSCAPE = {20, 60};
static final float[] DIMENSIONS_DIFF_PORTRAIT = {40, 60};
final float scale = getContext().getResources().getDisplayMetrics().density;
float[] dimensions =
(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
? DIMENSIONS_DIFF_LANDSCAPE : DIMENSIONS_DIFF_PORTRAIT;
addContentView(mContent, new LinearLayout.LayoutParams(
display.getWidth() - ((int) (dimensions[0] * scale + 0.5f)),
display.getHeight() - ((int) (dimensions[1] * scale + 0.5f))));
}
但是您仍然應該在onCreate上執行此操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.