更改方向更改时自定义对话框的尺寸
问题描述:
如何在方向更改时更改android中的对话框的尺寸。在的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))));
代码我想做同样的,当方向改变。请帮助
答
我建议你创建一个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了。
答
只是检查了这一点,它的自定义代码来设置对话框高度根据oriebtation:它通过实现这个我的对话成为灵活的工作对我来说...我M在平板电脑上使用这个..
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));
}