Android Custom ToolBar涵盖整个屏幕

时间:2017-01-19 17:26:28

标签: android tabs toolbar

我创建了一个自定义工具栏,显示在我的标签布局活动的顶部。将它包含在我的活动的xml中后,它将作为背景颜色(ORANGE)到我的标签布局的不同片段标签。

下面 enter image description here

ToolBar xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="@color/ColorPrimary"
    android:id="@+id/toolbar_clashmate"
    android:elevation="4dp">

</android.support.v7.widget.Toolbar>

TabLayout Activity的XML(在顶部添加工具栏的位置)

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



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="#dfdfdf"
    android:layout_height="match_parent"
    tools:context=".MainActivity">



    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/PrimaryColor"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/tabs"
        />



</RelativeLayout>
    <include
    android:id="@+id/tool_bar"
    layout="@layout/tool_bar"
    />

</FrameLayout>

如何将其设置为我可以进一步添加按钮的活动的顶部?

3 个答案:

答案 0 :(得分:2)

将工具栏更改为此

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:background="@color/ColorPrimary"
    android:id="@+id/toolbar_clashmate"
    android:elevation="4dp">

</android.support.v7.widget.Toolbar>

答案 1 :(得分:1)

您正在将android:layout_width="match_parent"用于放置在具有TabLayout)

的元素android:layout_height="wrap_content"的工具栏中

导致问题

使用此选项设置与默认ToolBar高度

对应的ActionBar的高度
android:layout_height="?android:attr/actionBarSize"

答案 2 :(得分:1)

您可以使用此代码

 <?xml version="1.0" encoding="utf-8"?>
 <android.support.design.widget.CoordinatorLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:background="#dfdfdf"
 android:layout_height="match_parent">

<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <include
        android:id="@+id/tool_bar"
        layout="@layout/testtoolbar"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>


</android.support.design.widget.AppBarLayout>


<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

 </LinearLayout>
 </android.support.design.widget.CoordinatorLayout>
相关问题