search

Home  >  Q&A  >  body text

让Android 5.0, 4.4 系统都实现沉淀式状态栏或透明状态栏的方法

我想在5.0 或者4.4 操作系统都实现沉淀式状态栏或透明状态栏,最好全部都采用style 的方式实现,最好不用这个开源库https://github.com/jgilfelt/SystemBarTint求解决思路

经过这几天的思考和研究,发现使用ActionBar,和不使用ActionBar,改为Toolbar 效果完全是不一样的,那在这两种情况下是否都可以完美的实现呢

高洛峰高洛峰2773 days ago666

reply all(3)I'll reply

  • PHPz

    PHPz2017-04-17 15:28:15

    For top view, you can use style,

    <style name="TopViewStyle">
        <item name="android:fitsSystemWindows">true</item>
        <item name="android:clipToPadding">true</item>
    </style>
    

    But the window flag still requires code to determine version support,

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    

    cannot be expressed in style, and style itself cannot make the difference in version identification.

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/top_text_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/TopViewStyle"/>
        <TextView
            android:id="@+id/example_text_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    

    MainActivity.java

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            }
        }
    
    }
    

    reply
    0
  • 阿神

    阿神2017-04-17 15:28:15

    My own solution: https://coding.net/u/tianshaokai/p/MaterialDesignDemo/git
    Two ways to implement Material Design in lower versions
    https://github.com/Witype/ SystemBarTintDemo
    https://github.com/Witype/ToolbarStatusDemo

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 15:28:15

    It is very simple to implement status bar immersion, and you only need to define the following theme in
    under values-v19 through style configuration styles.xml, and determine the application theme as this theme in Manifest.xml.

    <style name="AppTheme" parent="android:Theme.Holo.Light.NoActionBar">
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">false</item>
    </style>
    

    android:windowTranslucentStatus means the status bar is transparent
    android:windowTranslucentNavigation means the navigation bar (virtual button bar) is transparent

    Immersion is a trivial matter. The key trouble lies in the changes in the layout of layout and the optimization of fitsSystemWindows.
    I tell you from experience that SystemBarTint is already the simplest solution.

    reply
    0
  • Cancelreply