简体   繁体   English

带有自定义图标的Android ActionBar?

[英]Android ActionBar with custom Icons?

I want to create a Android ActionBar which looks like the following. 我想创建一个如下所示的Android ActionBar。

在此处输入图片说明

How can I build such a ActionBar. 我如何构建这样的ActionBar。 Is it possible to build it with Android material Design? 是否可以使用Android Material Design进行构建?

I would use the new ToolBar from Android Design Support Library 我将使用Android设计支持库中的新工具栏

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

Using ToolBar instead of ActionBar will give you more control on the elements themselves. 使用ToolBar而不是ActionBar将为您提供对元素本身的更多控制。

You can find a good article here: 您可以在这里找到一篇不错的文章:

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

在此处输入图片说明

app_bar.xml 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 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() 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 styles.xml

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

Technically you could stitch together such a layout with multiple elements. 从技术上讲,您可以将这样的布局与多个元素拼接在一起。 For example you have a header layout containing a TabLayout on the left and a Toolbar on the right side, instead of using the standard ActionBar. 例如,您有一个标题布局,在左侧包含一个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>

This example uses the AppCompat v7 and the Design Support library. 本示例使用AppCompat v7和设计支持库。

You may style the Views in a way (eg same background color) such that it looks as if its one single UI element. 您可以采用某种方式(例如,相同的背景颜色)对视图进行样式设置,以使其看起来好像是其单个UI元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM