StatusBar显示键盘何时显示

时间:2017-01-12 11:22:15

标签: android android-layout android-statusbar

我的活动使用了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方案(在清单上声明)。

2 个答案:

答案 0 :(得分:4)

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

ScrollView

除非您使用ScrollView包装活动内容,否则您无法在全屏活动中使用 SOFT_INPUT_ADJUST_RESIZE

为此,您需要将布局内容包装在<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>

 public class MapsMarkerActivity extends AppCompatActivity
    implements OnMapReadyCallback {
    // Include the OnCreate() method here too, as described above.
    @Override
    public void onMapReady(GoogleMap googleMap) {
        // Add a marker in Sydney, Australia,
        // and move the map's camera to the same location.
        LatLng sydney = new LatLng(-33.852, 151.211);
        googleMap.addMarker(new MarkerOptions().position(sydney)
            .title("Marker in Sydney"));
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}

告诉我它是否有效..

答案 1 :(得分:2)

要点: -

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

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

3)方法setSystemUiVisibility()仅在您调用它的视图可见时才有效。

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

您应该在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.
        }
    }
});