Android -- 案例學習積累

來源:互聯網
上載者:User

標籤:

Theme

ActionBar / ToolBar

android.support.design.widget.CollapsingToolbarLayout

android.support.design.widget.AppBarLayout

 

 

@、Theme

1、設定NoActionBar

<resources>    <!-- Base application theme. -->    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">        <!-- Customize your theme here. -->        <item name="colorPrimary">#3F51B5</item>        <!-- Light Indigo -->        <item name="colorPrimaryDark">#3949AB</item>        <!-- Dark Indigo -->        <item name="colorAccent">#00B0FF</item>        <!-- Blue -->    </style>    <style name="AppTheme" parent="AppTheme.Base"></style></resources>

 

 

@、ActionBar / ToolBar

1、Theme設定為NoActionBar,Layout檔案中添加

        <android.support.v7.widget.Toolbar                android:id="@+id/toolbar"                android:layout_width="match_parent"                android:layout_height="?attr/actionBarSize"                android:background="?attr/colorPrimary"                app:layout_scrollFlags="scroll|enterAlways"                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />    

然後再代碼中設定此Toolbar為ActionBar

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);        setSupportActionBar(toolbar); // 設定ActionBar        ActionBar supportedActionBar = getSupportActionBar();        if(supportedActionBar != null){            // 設定向上導航的表徵圖。預設為向左的箭頭            supportedActionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);            // 設定是否顯示向上導航: true顯示,false不顯示;預設為false.            supportedActionBar.setDisplayHomeAsUpEnabled(true);        }        

相關知識:

a、如果沒有在onOptionsItemSelected中對android.R.id.home進行處理,則:在AndroidManifest中相應的activity指定了android:parentActivityName,就返回到parentActivity,否則什麼都不做。

b、如果是返回parentActivity,則parentActivity會onDestroy, onCreate, onStart, onResume執行一遍。而如果是按返回鍵,則只是(onStart), onResume。對於主介面有Tab頁的時候要注意考慮這種情況。

c、在onOptionsItemSelected進行判斷,如果是android.R.id.home,則直接調用finishi(),這樣也可以到達按返回鍵的效果。當然如果onBackPressed()沒有做特殊處理的話,也可以調用onBackPressed()。

d、應用情境:使用此方法為側邊導覽列添加指標。

 

@、android.support.design.widget.CollapsingToolbarLayout

1、對Tookbar進行封裝。

2、如果collaspsingToolbarLayout的app:layout_scrollFlags設定為exitUntilCollasped,則對子控制項設定app:layout_collapseMode:pin(固定不動,Toolbar上的向上導航和功能表按鈕不會消失), parallax(隨著CollapsingToolbarLayout移動而移動,Toolbar上的向上導航和功能表按鈕會消失),不過最後的高度和寬度就是Toobar的高度和寬度。

3、app:contentScrim指定了最終顯示的內容。

 

@、android.support.design.widget.AppBarLayout

1、對子控制項設定app:layout_scrollFlags:sroll(向上滑(捲軸向下滑),子控制項一起向上滑), enterAlways(向下滑(捲軸向上滑),不管捲軸是否滑到頂,子控制項都出現,如果不指定該值,則要等捲軸滑到頂,子控制項才會出現), enterAlwaysCollasped(這個沒有測試出有什麼用??), exitUntilCollasped(結合CollapsingToolbarLayout使用,如果CollapsingToolbarLayout的Toolbar把app:layout_collapseMode設定為pin,則該Toolbar上的向上導航和功能表按鈕不會消失)。

樣本一:AppBarLayout + Toolbar + TabLayout,滑動時Toolbar隱藏,Tab標籤保留

       <android.support.design.widget.AppBarLayout            android:id="@+id/appbar"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">            <android.support.v7.widget.Toolbar                android:id="@+id/toolbar"                android:layout_width="match_parent"                android:layout_height="?attr/actionBarSize"                android:background="?attr/colorPrimary"                app:layout_scrollFlags="scroll|enterAlways"                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />            <android.support.design.widget.TabLayout                android:id="@+id/tabs"                android:layout_width="match_parent"                android:layout_height="wrap_content" />        </android.support.design.widget.AppBarLayout>    

 

樣本二: AppBarLayout + CollapsingToolbarLayout + Toolbar

    <android.support.design.widget.AppBarLayout        android:id="@+id/appbar"        android:layout_width="match_parent"        android:layout_height="256dp"        android:fitsSystemWindows="true"        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">        <android.support.design.widget.CollapsingToolbarLayout            android:id="@+id/collapsing_toolbar"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:fitsSystemWindows="true"            android:theme="@style/ThemeOverlay.AppCompat.Dark"            app:contentScrim="?attr/colorPrimary"            app:expandedTitleMarginEnd="72dp"            app:expandedTitleMarginStart="16dp"            app:layout_scrollFlags="scroll|exitUntilCollapsed">            <ImageView                android:id="@+id/image"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:fitsSystemWindows="true"                android:scaleType="centerCrop"                app:layout_collapseMode="parallax"                tools:background="@drawable/a"/>            <android.support.v7.widget.Toolbar                android:id="@+id/toolbar"                android:layout_width="match_parent"                android:layout_height="?attr/actionBarSize"                app:layout_collapseMode="parallax"                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>        </android.support.design.widget.CollapsingToolbarLayout>    </android.support.design.widget.AppBarLayout>

 

Android -- 案例學習積累

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.