【问题标题】:Material design toolbar on devices pre-lollipop棒棒糖前设备上的材料设计工具栏
【发布时间】:2014-12-29 14:30:06
【问题描述】:

我正在学习下一个教程:

我想做类似的设计:

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

values/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>

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

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

(第二个问题)

在我的布局上出现

"?attr/actionBarSize""?attr/colorPrimary"

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

(第三题)

当我这样做时,两个工具栏只出现一次,我希望一个在另一个里面显示内容。

我必须做什么才能显示两个工具栏?

谢谢

【问题讨论】:

    标签: android tablet android-5.0-lollipop


    【解决方案1】:

    我可以回答你的第一个问题。这很简单——你只需要阅读你的 IDE 给出的信息。您使用了错误的工具栏。您尝试传递给 setSupportActionBar-Method 的工具栏来自 android.widget 包。但是您必须使用 android.support.v7.widget 包中的工具栏。所以你的导入是错误的 - 纠正它们,你的代码应该可以工作。

    【讨论】:

      【解决方案2】:

      第一个问题解决了,让我试试第二个

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

      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" 并在color.xml 中定义colorPrimary 值。使用此站点获取不同的颜色值:Material Palette(我更喜欢此站点)。

      【讨论】:

        猜你喜欢
        • 2016-03-17
        • 1970-01-01
        • 2015-12-07
        • 2015-02-07
        • 2016-10-23
        • 2015-06-27
        • 2016-11-23
        • 1970-01-01
        • 2015-09-15
        相关资源
        最近更新 更多