2015-08-28 69 views
2

是否有簡單的方法添加工具欄下的視圖,而不會影響其當前的外觀?我只是想在工具欄文本的正下方添加一個額外的視圖。請注意,工具欄有陰影,只是在工具欄小部件下方添加視圖將無法達到我想要的效果。如何在工具欄下方添加視圖?

+0

發表您的xml文件 – Prachi

+0

@Prachi我沒有任何(還) ..我只使用默認的工具欄,只是想知道如何在其標題的文本視圖下添加視圖 –

+0

與將組件添加到「相對佈局」或「線性佈局」相同的方式 – Prachi

回答

2

使用此代碼來做到這一點。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top" 
android:orientation="vertical"> 

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/app_color" 
    android:titleTextAppearance="@android:color/white" 
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> 

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <!-- **** Place Your Content Here **** --> 


    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="My Content"/> 

    <!-- **** This is your view that appears below toolbar **** --> 
    <View 
     android:layout_width="match_parent" 
     android:layout_height="5dp" 
     android:background="@drawable/toolbar_dropshadow" /> 
</FrameLayout></LinearLayout> 

@toolbar_dropshadow

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
<gradient 
    android:startColor="@android:color/transparent" 
    android:endColor="#88333333" 
    android:angle="90"/> 
</shape> 

欲瞭解更多信息,請點擊此鏈接:https://stackoverflow.com/a/26904102/3907780

0
<android.support.v7.widget.Toolbar 
mlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/custom_tool_bar" 
     android:layout_width="fill_parent" 
     android:layout_height="@dimen/actionbar_height" 
     android:gravity="center"> 

     <RelativeLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 

//Add your components here 
</RelativeLayout> 

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

嘗試過它,它仍然出現在標題和菜單按鈕之間 –

+0

張貼您的xml和屏幕截圖 – Prachi

+0

黑色區域[截圖](http://i.imgur.com/qwXmup5.jpg)是我添加的視圖 –

0

什麼在上述職位的工作對我來說是改變佈局的工具欄是成線性佈局。

在使用框架佈局時,視圖位於工具欄下方。

0

如果您使用的是RelativeLayout的,使用方法:

<android.support.v7.widget.Toolbar 
mlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/custom_tool_bar" 
android:layout_width="fill_parent" 
android:layout_height="@dimen/actionbar_height" 
android:gravity="center"> 

<RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

//Add your components here, ie.: 
<MyView android:id="@+id/black_box" 
android:layout_below="@id/my_toolbar" //relativity left or right available too 
android:layout_width="match_parent" 
etc.etc./> 
</RelativeLayout> 

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

我不知道爲什麼你的工具欄高於佈局範圍,但嘗試,讓我們知道。

2

Toolbar像添加android:layout_height="?attr/actionBarSize"

<android.support.v7.widget.Toolbar 
         android:id="@+id/toolbar" 
         android:layout_width="match_parent" 
         android:layout_height="?attr/actionBarSize" 
         android:titleTextColor="#ffffff" 
         android:background="#ECA539" 
         app:layout_collapseMode="pin" 
         app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

,然後在Relativelayout添加android:layout_marginTop="?attr/actionBarSize"象下面這樣:

<RelativeLayout 

       app:layout_behavior="@string/appbar_scrolling_view_behavior" 
       android:layout_width="match_parent" 
       android:layout_marginTop="?attr/actionBarSize" 
       android:layout_height="wrap_content">