2017-01-19 10 views
-2

私は、自分のタブレイアウトアクティビティの上部に表示するカスタムツールバーを作成しました。私のアクティビティのxmlにそれを含めた後、それは、自分のタブレイアウトの異なるフラグメントタブに背景色(ORANGE)として機能します。Android Custom ToolBarは画面全体をカバーします

ここ enter image description here

ツールバー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活動の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> 

さらにそれにボタンを追加できますか?

+0

を使用することができます。代わりにラップコンテンツを使用する –

+0

ツールバーに 'android:layout_width =" match_parent "'と 'android:layout_height ="?attr/actionBarSize "'を使用する – rafsanahmad007

答えて

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

あなたはActionBar高さの要素の中に入れツールバーにandroid:layout_width="match_parent"(これがデフォルトに対応するToolBarの高さを設定するには、問題

使用を原因とandroid:layout_height="wrap_content"

を持ってTabLayout)を使用している

android:layout_height="?android:attr/actionBarSize" 
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> 
関連する問題