1.沉浸式布局的实现
在res/values/styles.xml文件中实现代码:
<resources>
<style name="AppTheme" parent="@style/BaseTheme">
</style>
<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
</style>
</resources>
在res文件夹下新建文件夹values-v19,在values-v19文件下新建文件style文件内容:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="@style/BaseTheme">
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
</resources>
//说明:windowTranslucentNavigation值为false时底部虚拟按键,黑色背景为true的可以根据布局来更改虚拟按键的背景颜色。
在android的配置文件中AndroidManifest.xml中设置主题:
<application
android:name=".BaseApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:windowSoftInputMode="adjustResize|stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
以上能够实现沉浸式布局。
android:windowTranslucentNavigation"=true android:windowTranslucentNavigation"=false
输入框在底部时,在弹出输入法时,会将顶部的标题栏顶出界面,为了解决这样的问题使用一个
AndroidBug5497Workaround类:
public class AndroidBug5497Workaround {
public static void assistActivity(Activity content) {
new AndroidBug5497Workaround(content);
}
private View mChildOfContent;
private int usableHeightPrevious;
private ViewGroup.LayoutParams frameLayoutParams;
private AndroidBug5497Workaround(Activity activity) {
FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
mChildOfContent = content.getChildAt(0);
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
possiblyResizeChildOfContent();
}
});
frameLayoutParams = mChildOfContent.getLayoutParams();
}
private void possiblyResizeChildOfContent() {
int usableHeightNow = computeUsableHeight();
if (usableHeightNow != usableHeightPrevious) {
//如果两次高度不一致
//将计算的可视高度设置成视图的高度
frameLayoutParams.height = usableHeightNow;
mChildOfContent.requestLayout();//请求重新布局
usableHeightPrevious = usableHeightNow;
}
}
private int computeUsableHeight() {
//计算视图可视高度
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
return (r.bottom);
}
}
需要在AndroidManifest该activity下添加:
android:windowSoftInputMode="adjustResize|stateHidden"
在该activity代码界面,在setContentView语句下添加
AndroidBug5497Workaround.assistActivity(MainActivity.this);