關于StatusBar適配MD主題整理

主題使用:

  • 使用Theme.AppCompat.Light.NoActionBar(toolbar的兼容主題)

關于fitsSystemWindows 屬性介紹:

  • 如果你希望拓展的區(qū)域不被狀態(tài)欄遮擋住使用:fitsSystemWindows屬性
  • fitsSystemWindows 按照深度優(yōu)先的方式其作用孽拷,所以使用該屬性的 View 的順序是有要求的怀喉,如果第一個 View 使用了 inset (系統(tǒng)窗口的尺寸)則會導致其他 View 尺寸不一樣。
  • Inset 總是相對于全屏幕的获高,Inset 可能在 View layout 之前就已經(jīng)應用了哈肖,所以在設置 View 的 padding 之前 View 并不知道其具體相對于系統(tǒng)窗口的位置。
  • View 的其他 padding 值被重新改寫了念秧,在使用 fitsSystemWindows 為 true 的View 上設置 padding 值(paddingLeft/paddingTop/ 等)是沒有效果的淤井。
  • 比如 你想把 RecyclerView 的內(nèi)容顯示在一個透明導航欄的下面,就類似于 Google Now 一樣摊趾,你可以在 RecyclerView 上設置 android:fitsSystemWindows=”true” 币狠,然后在設置 RecyclerView 的 android:clipToPadding=”false”,這樣這個 RecyclerView 就會顯示在導航欄下方了严就,當你向上滑動 RecyclerView 到底的時候, RecyclerView 內(nèi)容在導航欄上方器罐,并沒有被導航欄擋住梢为。
  • 使用fitSystemWindows屬性讓系統(tǒng)幫我們自動適配不同情況下的status bar,讓我們的view的paddingTop獲取到一個合理的值轰坊。(還有其他的方案是通過手動設置paddingTop的值來進行適配的:在values-v19里設置paddingTop值為25dp铸董,在values里設置為0dp,但是在某些自定義的rom里status bar的高度是被有修改過的。還有就是通過自定義繼承toolbar肴沫,在代碼里動態(tài)獲取status bar的高度并設置paddingTop的值粟害,但這樣又弄得太麻煩了)。

code

BaseActivity
public class BaseActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //系統(tǒng)版本的校驗
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        /**
         * 使用:FLAG_TRANSLUCENT_STATUS標志位颤芬,activity布局會擴展到狀態(tài)欄
         * 
         */

        //設置參數(shù)悲幅,將status設置成透明
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//添加statusbar 透明
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);// 在存在虛擬按鍵的手機上,導航欄為透明
    }
  }
}
MainActivity
public class MainActivity extends BaseActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    /**
     * 自定義toolbar的時候可以將下面注釋掉站蝠,
     * 在系統(tǒng)布局中使用colorPrimary和minHeight指定顏色和高度
     */
    //將toolbar設置成actionbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
    setSupportActionBar(toolbar);

  }
}
activity_main.xml
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context="com.example.leiiiooo.immersivedemo.MainActivity">
  <!--toolbar-->
  <!--<include
  layout="@layout/mytoolbar_layout"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" />-->
  <!--自定義topbar-->
  <include
    layout="@layout/mytopbar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />


  <TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:text="ThinkCool" />

</LinearLayout>
mytoolbar_layout.xml
<android.support.v7.widget.Toolbar
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/my_toolbar"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@color/colorPrimary"
  android:fitsSystemWindows="true"
  android:minHeight="?attr/actionBarSize"
  android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
  app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
mytopbar_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="@color/colorPrimary"
          android:minHeight="?attr/actionBarSize"
          android:gravity="center"
          android:fitsSystemWindows="true"
          android:orientation="vertical">

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp"
    android:gravity="center"
    android:orientation="horizontal">
    <ImageView
        android:id="@+id/top_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/top_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:gravity="center"
        android:text="登陸"
        android:textSize="16sp" />

    <ImageView
        android:id="@+id/top_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher" />
  </LinearLayout>

</LinearLayout>
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.leiiiooo.immersivedemo">
  <!--設置成沒有actionbar的主題-->
  <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity android:name=".BaseActivity">
    </activity>
  </application>

</manifest>

效果如圖:

使用系統(tǒng)toolbar
自定義toolbar+fitsSystemWindows
自定義toolbar 未設置fitsSystemWindows
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末汰具,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子菱魔,更是在濱河造成了極大的恐慌留荔,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件澜倦,死亡現(xiàn)場離奇詭異聚蝶,居然都是意外死亡杰妓,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進店門碘勉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來巷挥,“玉大人,你說我怎么就攤上這事恰聘【涓鳎” “怎么了?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵晴叨,是天一觀的道長凿宾。 經(jīng)常有香客問我,道長兼蕊,這世上最難降的妖魔是什么初厚? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮孙技,結果婚禮上产禾,老公的妹妹穿的比我還像新娘。我一直安慰自己牵啦,他們只是感情好亚情,可當我...
    茶點故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著哈雏,像睡著了一般楞件。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上裳瘪,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天土浸,我揣著相機與錄音,去河邊找鬼彭羹。 笑死黄伊,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的派殷。 我是一名探鬼主播还最,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼毡惜!你這毒婦竟也來了憋活?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤虱黄,失蹤者是張志新(化名)和其女友劉穎悦即,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡辜梳,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年粱甫,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片作瞄。...
    茶點故事閱讀 40,505評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡茶宵,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出宗挥,到底是詐尸還是另有隱情乌庶,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布契耿,位于F島的核電站瞒大,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏搪桂。R本人自食惡果不足惜透敌,卻給世界環(huán)境...
    茶點故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望踢械。 院中可真熱鬧酗电,春花似錦、人聲如沸内列。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽话瞧。三九已至嫩与,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間移稳,已是汗流浹背蕴纳。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工会油, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留个粱,地道東北人。 一個月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓翻翩,卻偏偏與公主長得像都许,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子嫂冻,可洞房花燭夜當晚...
    茶點故事閱讀 45,515評論 2 359

推薦閱讀更多精彩內(nèi)容