简体   繁体   English

StatusBar显示键盘何时显示

[英]StatusBar shows when keyboard shows up

My activity use CollapsingToolbarLayout and i remove successfully the StatusBar doing this: 我的活动使用了CollapsingToolbarLayout,我成功删除了StatusBar

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

The problem is that when the softkeyboard pops up, the StatusBar appear again, consuming precious space. 问题是,当softkeyboard弹出时, StatusBar会再次出现,占用宝贵的空间。

How to keep it hide permanently on the ActivityView ? 如何让它永久隐藏在ActivityView

If i set it to FullScreen, it works but i loose the SOFT_INPUT_ADJUST_RESIZE parameter. 如果我将其设置为FullScreen,它可以工作,但我松开了SOFT_INPUT_ADJUST_RESIZE参数。 The main window scrolls when softkeyboard appears, instead of resize as needed. 当出现softkeyboard时,主窗口会滚动,而不是根据需要调整大小。

Update: 更新:

<?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>

The code: 编码:

 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);
}

The activity is using NoActionBar scheme(declared on the manifest). 该活动使用NoActionBar方案(在清单上声明)。

Add this line in your styles activity style 在样式活动样式中添加此行

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

Unless you wrap your activity content with ScrollView you cant use SOFT_INPUT_ADJUST_RESIZE in a full screen activity 除非使用ScrollView包装活动内容,否则无法在全屏活动中使用SOFT_INPUT_ADJUST_RESIZE

For that to work you need to wrap your layout content inside ScrollView 为此,您需要将您的布局内容包装在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>

tell me if it works.. 告诉我它是否有效..

Points to be noted :- 需要注意的要点: -

1) Once UI flags have been cleared (for example, by navigating away from the activity), your app needs to reset them if you want to hide the bars again. 1)一旦UI标志被清除(例如,通过导航离开活动),如果您想再次隐藏栏,您的应用程序需要重置它们。 See Responding to UI Visibility Changes for a discussion of how to listen for UI visibility changes so that your app can respond accordingly. 有关如何监听UI可见性更改的讨论,请参阅响应UI可见性更改,以便您的应用可以相应地做出响应。

2) Where you set the UI flags makes a difference. 2)你设置UI标志的地方有所不同。 If you hide the system bars in your activity's onCreate() method and the user presses Home, the system bars will reappear. 如果在活动的onCreate()方法中隐藏系统栏并且用户按下Home,系统栏将重新出现。 When the user reopens the activity, onCreate() won't get called, so the system bars will remain visible. 当用户重新打开活动时,onCreate()将不会被调用,因此系统栏将保持可见。 If you want system UI changes to persist as the user navigates in and out of your activity, set UI flags in onResume() or onWindowFocusChanged(). 如果您希望在用户导入和导出活动时保持系统UI更改,请在onResume()或onWindowFocusChanged()中设置UI标记。

3) The method setSystemUiVisibility() only has an effect if the view you call it from is visible. 3)方法setSystemUiVisibility()仅在您调用它的视图可见时才有效。

4) Navigating away from the view causes flags set with setSystemUiVisibility() to be cleared. 4)导航离开视图会导致使用setSystemUiVisibility()设置的标志被清除。

You should add this code on create 您应该在create上添加此代码

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.
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM