当前位置: 移动技术网 > IT编程>移动开发>Android > Android分屏多窗口的实践代码

Android分屏多窗口的实践代码

2019年07月24日  | 移动技术网IT编程  | 我要评论

拿着烟斗的男孩歌词,色痞超警,郑博士

android n 中推出了多窗口支持,项目要求适配多窗口模式,记录一下。

1.生命周期:对于完全没有适配多窗口的app来说,当启用多窗口模式或者拖动窗口时,会出现activity销毁重新加载的现象。 解决方法:在manifest文件里面给activity加上下面一行属性

android:configchanges="screensize|smallestscreensize|screenlayout|orientation"

2.当启用多窗口模式时,可能有改变原ui的需求,这时可以在代码里面修改:

 @override
 public void onmultiwindowmodechanged(boolean isinmultiwindowmode) {
 super.onmultiwindowmodechanged(isinmultiwindowmode);
 //在此处根据isinmultiwindowmode来适配多窗口ui
 }

onmultiwindowmodechanged在activity,fragment里面都有,会在多窗口模式变化的时候调用该方法。不过注意了,当已经是多窗口模式时,进入新页面,并不会调用该方法。那怎么办呢?可以用下面这个方法来判断当前是否是多窗口状态,最低兼容sdk版本是24:

activity().isinmultiwindowmode()

可以用activity的这个方法判断。当是多窗口时隐藏或者显示某些布局。

3.如果要在拖动多窗口的过程中,动态改变布局,可以在manifest文件里面对相应的activity配置,例如:

<activity
  android:name="com.android.multiwindowplayground.activities.minimumsizeactivity"
  android:launchmode="singleinstance"
  android:taskaffinity="">
  <layout
  android:defaultheight="500dp"
  android:defaultwidth="750dp"
  android:gravity="top|end"
  android:minwidth="500dp"
  android:minheight="500dp" />
 </activity>

然后在activity的布局文件里面,设置线性布局的layout_gravity和权重:

<linearlayout android:id="@+id/layout"
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@color/white"
 android:orientation="vertical">
 <scrollview
 android:id="@+id/scrollview"
 android:layout_width="match_parent"
 android:layout_height="0dp"
 android:layout_gravity="top"
 android:layout_weight="0.75">
 <textview
  android:id="@+id/description"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:paddingbottom="@dimen/activity_vertical_margin"
  android:paddingleft="@dimen/activity_horizontal_margin"
  android:paddingright="@dimen/activity_horizontal_margin"
  android:paddingtop="@dimen/activity_vertical_margin"
  android:textcolor="@color/white" />
 </scrollview>
 <include
 layout="@layout/logging"
 android:layout_width="match_parent"
 android:layout_height="0dp"
 android:layout_gravity="bottom"
 android:layout_weight="0.25" />
</linearlayout>

就会在拖动的过程中自动改变。这是android官方的一个demo。

4.在拖动多窗口的过程中,也可以在代码里面动态设置,会调用onconfigurationchanged方法,在configuration newconfig参数中有很多信息,就有当前应用的宽高信息。

@override
 public void onconfigurationchanged(configuration newconfig) {
 super.onconfigurationchanged(newconfig);
 int screenwidthdp=newconfig.screenwidthdp;
 int screenheightdp=newconfig.screenheightdp;
 //根据宽高动态改变布局
 }

最后推荐:android官方文档

以上所述是小编给大家介绍的android分屏多窗口的实践代码,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网