当键盘出现时,状态栏显示



我的活动使用CollapsingToolbarLayout,我成功地删除了StatusBar,这样做:

View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);

问题是,当softkeyboard弹出时,StatusBar再次出现,占用了宝贵的空间。

如何将其永久隐藏在ActivityView上?

如果我将其设置为FullScreen,它可以工作,但我松开了SOFT_INPUT_ADJUST_RESIZE参数。当softkeyboard出现时,主窗口将滚动,而不是根据需要调整大小。

更新:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/my_appar"
android:fitsSystemWindows="false"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="@color/white">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:toolbarId="@+id/collapsetoolbar"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
app:layout_scrollInterpolator="@android:anim/decelerate_interpolator"
app:contentScrim="?attr/colorPrimary">
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:elevation="4dp"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="... .MyActivity"
android:orientation="vertical"
>
<FrameLayout
android:id="@+id/yt_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<fragment
...
</FrameLayout>
<com.byte_artisan.mchat.compoundViews.chatview.ChatView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/fragment1"
android:id="@+id/chat_view_id">
</com.byte_artisan.mchat.compoundViews.chatview.ChatView>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

代码:

protected void onCreate(Bundle savedInstanceState) {        
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mchat);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE );
..
}

@Override
protected void onResume() {
super.onResume();
View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
}

该活动正在使用NoActionBar方案(在清单上声明)。

在样式活动样式中添加此行

<item name="android:windowFullscreen">true</item>

除非用ScrollView包装活动内容,否则不能在全屏活动中使用SOFT_INPUT_ADJUST_RESIZE

为此,您需要将布局内容封装在ScrollView

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- everything you already have -->
</LinearLayout>

告诉我它是否有效。。

需要注意的点:-

1) 一旦UI标志被清除(例如,通过导航离开活动),如果你想再次隐藏栏,你的应用程序需要重置它们。请参阅响应UI可见性更改,了解如何侦听UI可见性更改以便您的应用程序做出相应响应的讨论。

2) 设置UI标志的位置会有所不同。如果在活动的onCreate()方法中隐藏系统栏,并且用户按Home,系统栏将重新出现。当用户重新打开活动时,不会调用onCreate(),因此系统栏将保持可见。如果您希望系统UI更改在用户进出活动时保持不变,请在onResume()或onWindowFocusChanged()中设置UI标志。

3) 方法setSystemUiVisibility()只有在从中调用它的视图可见时才有效果。

4) 导航离开视图会导致使用setSystemUiVisibility()设置的标志被清除。

您应该在创建时添加此代码

View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener
(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
// Note that system bars will only be "visible" if none of the
// LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
// TODO: The system bars are visible. Make any desired
// adjustments to your UI, such as showing the action bar or
// other navigational controls.
// again hide it
} else {
// TODO: The system bars are NOT visible. Make any desired
// adjustments to your UI, such as hiding the action bar or
// other navigational controls.
}
}
});

最新更新