设备上的材料设计工具栏预棒棒糖

时间:2014-12-29 14:30:06

标签: android tablet android-5.0-lollipop

我正在学习下一个教程:

我想做类似的设计:

md

我知道我需要使用两个工具栏。

值/的themes.xml

<resources>
<style name="Theme.MiThema" parent="Theme.AppCompat.Light">
    <!-- Here we setting appcompat’s actionBarStyle -->
    <!--<item name="actionBarStyle">@style/MyActionBarStyle</item>-->


    <!-- ...and here we setting appcompat’s color theming attrs -->
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>

    <!-- colorAccent is used as the default value for colorControlActivated
         which is used to tint widgets -->
    <item name="colorAccent">@color/accent</item>
</style>
    </resources>

class.java

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.Toolbar;

public class RegistrarInciTraActivity extends ActionBarActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registrar_inci_traf_layout);

        //ocultamos ActionBar
        getSupportActionBar().hide();


       Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
       setSupportActionBar(toolbar);
    }
}

布局

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="horizontal">

    <!-- The toolbar -->
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary" />

    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/my_drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- drawer view -->
        <LinearLayout
            android:layout_width="304dp"
            android:layout_height="match_parent"
            android:layout_gravity="left|start">

            <!-- drawer content -->

        </LinearLayout>

        <!-- normal content view -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <!-- The rest of content view -->

        </LinearLayout>
    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

Tha代码报告“setSupportActionBar(toolbar);”上的一个错误:

md2

(第一个问题)¿有什么问题? (已解决)

(第二个问题)

在我的布局上出现

  

“?attr / actionBarSize”“?attr / colorPrimary”

我必须在哪里声明这些属性?

(第三个问题)

当我做两个工具栏时,只显示一个工具栏,我想要一个在另一个工具栏中显示内容。

md3

我必须显示两个工具栏吗?

由于

2 个答案:

答案 0 :(得分:2)

我可以帮你解决第一个问题。它非常简单 - 您只需阅读IDE提供的消息。您使用的是错误的工具栏。您尝试传递给setSupportActionBar-Method的工具栏来自android.widget包。但是你要使用android.support.v7.widget包中的工具栏。所以你的导入是错误的 - 纠正它们,你的代码应该有效。

答案 1 :(得分:1)

当第一个问题解决后,让我尝试第二个一个,

首先,您不必在任何地方定义这些值,它们是预定义的。

  1. “?attr / actionBarSize” :这是Android提供的默认值。你将在android.support.v7.appcompat/R得到它,但它会给你一个int引用。如果您想知道Actionbar的实际高度,则必须在运行时解析属性actionBarSize。请点击此链接:Get ActionBar Size(在其他SO Q上给出答案)。 此外,通过 Material Design ,Google在Material Design中设计新应用程序(或升级旧应用程序)时共享了一些标准。您可以在此处遵循这些规范:LayoutMetrics & keylines

  2. “?attr / colorPrimary” :这也是一个预先定义的值。你可以在android.support.v7.appcompat/R得到它,但它会给你一个int引用。但是,如果您想为自己的ToolBar添加自定义背景颜色,则只需在android:background="@color/colorPrimary"中添加适当的调色板colorPrimary并定义color.xml值即可。使用此网站获取不同的颜色值:Material Palette(我更喜欢这个网站)。

相关问题