android 横竖屏切换怎么换另一个dialog
1个回答
展开全部
1、根据你的需要写一个根view的onLayout方法,如下,并写一个回调接口供dialog实现,我这里直接把dialog传进来了。
public class MiddleView extends RelativeLayout {
private CreditsWallDialog mDialog;
public MiddleView(Context context, CreditsWallDialog dialog) {
super(context);
this.mDialog = dialog;
}
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
super.onLayout(changed, left, top, right, bottom);
mDialog.onLayoutCallBack(left, top, right, bottom);
}
}
2、dialog的layout中把MiddleView作为根视图使用,如果是代码布局的话可以这样 setContentView(new MiddleView(mContext, this));
<?xml version="1.0" encoding="utf-8"?>
<cn.richinfo.jifenqiang.widget.MiddleView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- 这里添加自己的控件 -->
</cn.richinfo.jifenqiang.widget.MiddleView>
3、在dialog中实现步骤1中的回调方法
复制代码
public void onLayoutCallBack(int left, int top, int right, int bottom) {
DisplayWindow win = DisplayWindow.getDisplayWindow(mContext);
int width = (int) ((double) win.width * scale_width);
int height = (int) ((double) win.height * scale_height);
if (width == this.mWidth && height == this.mHeight) {
LogUtils.println("lcq:onLayCallbck is same to last...");
return;
}
setWindowAttribute(width, height);
}
复制代码
4、重新设置windows的宽度和高度
复制代码
private void setWindowAttribute(int width, int height) {
Window window = getWindow();
android.view.WindowManager.LayoutParams windowParams = window
.getAttributes();
windowParams.width = width;
windowParams.height = height;
DisplayWindow dWin = DisplayWindow.getDisplayWindow(mContext);
int adjustPix = dWin.dipToPix(16);
windowParams.width += adjustPix;
windowParams.height += adjustPix;
if (windowParams.width > dWin.width) {
windowParams.width = dWin.width;
}
if (windowParams.height > dWin.height) {
windowParams.height = dWin.height;
}
this.mWidth = width;
this.mHeight = height;
window.setAttributes(windowParams);
}
public class MiddleView extends RelativeLayout {
private CreditsWallDialog mDialog;
public MiddleView(Context context, CreditsWallDialog dialog) {
super(context);
this.mDialog = dialog;
}
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
super.onLayout(changed, left, top, right, bottom);
mDialog.onLayoutCallBack(left, top, right, bottom);
}
}
2、dialog的layout中把MiddleView作为根视图使用,如果是代码布局的话可以这样 setContentView(new MiddleView(mContext, this));
<?xml version="1.0" encoding="utf-8"?>
<cn.richinfo.jifenqiang.widget.MiddleView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- 这里添加自己的控件 -->
</cn.richinfo.jifenqiang.widget.MiddleView>
3、在dialog中实现步骤1中的回调方法
复制代码
public void onLayoutCallBack(int left, int top, int right, int bottom) {
DisplayWindow win = DisplayWindow.getDisplayWindow(mContext);
int width = (int) ((double) win.width * scale_width);
int height = (int) ((double) win.height * scale_height);
if (width == this.mWidth && height == this.mHeight) {
LogUtils.println("lcq:onLayCallbck is same to last...");
return;
}
setWindowAttribute(width, height);
}
复制代码
4、重新设置windows的宽度和高度
复制代码
private void setWindowAttribute(int width, int height) {
Window window = getWindow();
android.view.WindowManager.LayoutParams windowParams = window
.getAttributes();
windowParams.width = width;
windowParams.height = height;
DisplayWindow dWin = DisplayWindow.getDisplayWindow(mContext);
int adjustPix = dWin.dipToPix(16);
windowParams.width += adjustPix;
windowParams.height += adjustPix;
if (windowParams.width > dWin.width) {
windowParams.width = dWin.width;
}
if (windowParams.height > dWin.height) {
windowParams.height = dWin.height;
}
this.mWidth = width;
this.mHeight = height;
window.setAttributes(windowParams);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询