Android自定义工具栏覆盖整个屏幕
我创建了一个自定义工具栏,显示在选项卡布局活动的顶部。将其包含在我的活动的xml中后,它将作为选项卡布局的不同片段选项卡的背景色(橙色) 这里 工具栏xmlAndroid自定义工具栏覆盖整个屏幕,android,tabs,toolbar,Android,Tabs,Toolbar,我创建了一个自定义工具栏,显示在选项卡布局活动的顶部。将其包含在我的活动的xml中后,它将作为选项卡布局的不同片段选项卡的背景色(橙色) 这里 工具栏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="ma
<?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活动的XML(在顶部添加工具栏的位置)
如何将其设置为活动的顶部,以便我可以进一步向其添加按钮?您正在使用
android:layout\u width=“match\u parent”
将工具栏放置在元素(TabLayout)
中,该元素具有android:layout\u height=“wrap\u content”
这就是问题的根源
使用此选项可设置与默认的ActionBar
高度相对应的工具栏的高度
android:layout_height="?android:attr/actionBarSize"
将工具栏更改为此
<?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>
您可以使用此代码
<?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>
您已经清楚地提到了工具栏的高度作为匹配父级。使用Wrap content insteaduseandroid:layout\u width=“match\u parent”
和android:layout\u height=“?attr/actionBarSize”
用于工具栏
<?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>