【问题标题】:Toolbar is not shown in android application工具栏未显示在 android 应用程序中
【发布时间】:2015-09-29 23:36:43
【问题描述】:

我最近开始将我的旧应用迁移到 Material Design,并决定使用工具栏而不是操作栏来进行更多自定义。我尝试使用以下代码将工具栏添加到仅一个活动:

layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="48dip"
        android:background="@drawable/background_tabs" />

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tabs"
        tools:context=".MainActivity" />

</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private ViewPager pager;
    private MyPagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyApp.tracker().send(new HitBuilders.EventBuilder("ui", "open")
        .setLabel("main")
        .build());
        // Set a ToolBar to replace the ActionBar.
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
}

}

style.xml

<style name="MyTheme.Base" parent="@style/Theme.AppCompat.Light.NoActionBar">

        <!-- your app branding color for the app bar -->
        <item name="colorPrimary">@color/redLatest</item>
        <!-- darker variant for the status bar and contextual app bars -->
        <item name="colorPrimaryDark">@color/redLatest_dark</item>
        <item name="colorAccent">@color/redLatest</item>
        <item name="actionBarStyle">@style/MyActionBar</item>
        <item name="actionDropDownStyle">@style/MyActionBarDropDown</item>
    </style>

现在,当我运行上述代码时,我的活动中没有操作栏,这很好,但工具栏也没有显示,这很奇怪。我知道我可能会犯一些我无法找出的愚蠢错误。请帮助

【问题讨论】:

    标签: android material-design android-appcompat android-toolbar


    【解决方案1】:

    您已经在RelativeLayout 中添加了视图,因此您应该指定它们的位置。由于您没有为TabLayout 指定它,所以TablayoutToolbar 重叠。只需在TabLayout 中添加android:layout_below="@id/toolbar"

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_below="@id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="48dip"
        android:background="@drawable/background_tabs" />
    

    【讨论】:

      【解决方案2】:

      使用这个

      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent" >
      
      <android.support.v7.widget.Toolbar
          android:id="@+id/toolbar"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="?attr/colorPrimary"
          android:minHeight="?attr/actionBarSize" >
      </android.support.v7.widget.Toolbar>
      
      <android.support.design.widget.TabLayout
          android:id="@+id/tabs"
          android:layout_below="@+id/toolbar"
          android:layout_width="match_parent"
          android:layout_height="48dip"
          android:background="@drawable/background_tabs" />
      
      <android.support.v4.view.ViewPager
          android:id="@+id/pager"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_below="@+id/tabs"
          tools:context=".MainActivity" />
      

      【讨论】:

        猜你喜欢
        • 2015-07-07
        • 2017-05-10
        • 1970-01-01
        • 2015-02-04
        • 1970-01-01
        • 2016-08-28
        • 1970-01-01
        • 2015-03-31
        • 2019-02-10
        相关资源
        最近更新 更多