簡體   English   中英

帶有自定義圖標的Android ActionBar?

[英]Android ActionBar with custom Icons?

我想創建一個如下所示的Android ActionBar。

在此處輸入圖片說明

我如何構建這樣的ActionBar。 是否可以使用Android Material Design進行構建?

我將使用Android設計支持庫中的新工具欄

http://android-developers.blogspot.it/2015/05/android-design-support-library.html

使用ToolBar而不是ActionBar將為您提供對元素本身的更多控制。

您可以在這里找到一篇不錯的文章:

http://www.android4devs.com/2014/12/how-to-make-material-design-app.html

在此處輸入圖片說明

app_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/appBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#3F51B5"
    android:theme="@style/ThemeOverlay.AppCompat.Dark" >
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.design.widget.TabLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:id="@+id/tabLayout"/>
    </FrameLayout>
</android.support.v7.widget.Toolbar>

main_activity_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".MainActivity">
    <include layout="@layout/app_bar" />
</RelativeLayout>

main_activity_onCreate()

toolBar = (Toolbar) findViewById(R.id.appBar);
toolBar.setTitle("Titolo");
setSupportActionBar(toolBar);

tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.user));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.user_group_1));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.user_group_2));

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
</style>

從技術上講,您可以將這樣的布局與多個元素拼接在一起。 例如,您有一個標題布局,在左側包含一個TabLayout,在右側包含一個Toolbar,而不是使用標准的ActionBar。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:orientation="horizontal">

    <android.support.design.widget.TabLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <android.support.v7.widget.Toolbar
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>

本示例使用AppCompat v7和設計支持庫。

您可以采用某種方式(例如,相同的背景顏色)對視圖進行樣式設置,以使其看起來好像是其單個UI元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM