2015-09-06 218 views
1

我已經創建了兩個頂部和底部的工具欄。底部工具欄佈局

,結果是這樣的:

enter image description here

我的問題是,如何才能讓我的底部工具欄的項目爲中心,其寬度跨越垂直,以適應工具欄?

這裏是我做了什麼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="app.shinobi.org.ssmis.MainActivity"> 

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

    <LinearLayout 
     android:id="@+id/container_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <include 
      android:id="@+id/toolbar" 
      layout="@layout/main_toolbar" /> 
    </LinearLayout> 

    <app.shinobi.org.util.tab.SlidingTabLayout 
     android:id="@+id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:elevation="2dp" 
     android:background="@color/primaryColor"/> 

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

    <include 
     android:id="@+id/stats_toolbar" 
     layout="@layout/stats_toolbar" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:layout_alignParentBottom="true" 
     /> 
</LinearLayout> 

這些是我的兩個菜單:

的頂部工具欄:

<menu 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" 
tools:context="app.shinobi.org.ssmis.MainActivity"> 

<item android:id="@+id/search" android:title="@string/search" 
    android:icon="@drawable/ic_search" android:orderInCategory="1" app:showAsAction="always"/> 
<item android:id="@+id/refresh" android:title="@string/action_refressh" 
    android:icon="@drawable/ic_refresh" android:orderInCategory="2" app:showAsAction="always" /> 

爲底端 工具欄:

<menu 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" 
tools:context="app.shinobi.org.ssmis.MainActivity"> 

<item android:id="@+id/monthly" android:title="@string/action_monthly" 
    android:icon="@drawable/ic_refresh" android:orderInCategory="3" app:showAsAction="always" /> 
<item android:id="@+id/yearly" android:title="@string/action_yearly" 
    android:icon="@drawable/ic_refresh" android:orderInCategory="4" app:showAsAction="always" /> 
<item android:id="@+id/drugs" android:title="@string/action_drugs" 
    android:icon="@drawable/ic_refresh" android:orderInCategory="5" app:showAsAction="always" /> 

謝謝!

+0

你嘗試應用重力stats_toolbar的中心? – JoxTraex

+0

你的應用程序有一個很酷的設計! –

+0

@ V.Kalyuzhnyu,認真嗎?不過謝謝。 – iamjc015

回答

2

創建菜單總是會將您的按鈕放到工具欄的末尾 如果喲ü要居中這些按鈕,你將不得不使用toolbar.addView(view)您的視圖可以從XML文件中像這樣吹:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:weightSum="3" 
    > 

    <ImageButton 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     /> 

    <ImageButton 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     /> 

    <ImageButton 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     /> 

</LinearLayout> 

然後你只需要:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     toolbar.setContentInsetsRelative(0, 0); 
     toolbar.addView(
       LayoutInflater.from(this).inflate(R.layout.toolbar, null, false), 
       new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT) 
     ); 
+0

虐待這個,所以我怎麼處理這裏的事件? – iamjc015

+0

我會將此標記爲答案:3但是,請問您可以添加如何處理事件。雖然,我已經解決了,也許其他人可能需要它。謝謝! – iamjc015

1

我可以建議一個不是很好的解決方案,但它的工作原理。如果我理解包含佈局,你有一個工具欄視圖。它是一個容器,因此您可以像使用相對或線性佈局一樣使用它。並在其中放置按鈕或其他視圖。並在中心寫如重力的屬性。就這些。

另一種方法 - 做菜單佈局中的項目可能是。但我不知道如何(

PS:短例如

<android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:minHeight="?attr/actionBarSize" 
     android:background="@color/primary" 
     app:layout_scrollFlags="scroll|enterAlways" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" > 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="previousActivity" 
      android:src="@drawable/ic_navigate_before_white_24dp"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Contact us" 
      android:textColor="@color/white" 
      android:textSize="20sp" 
      android:layout_gravity="center" 
      android:id="@+id/toolbar_title" /> 

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

你可以參考我的一些鏈接或其他...謝謝! – iamjc015

1

其實你可以使用一個ActionMenuView,以取代底部工具欄

或者你可以試試這個SplitToolbar實施

https://gist.github.com/dodgex/7bc81fd2cbb70a8d5117

+0

我會搜索ActionMenuView,稍後再回復。 – iamjc015

+0

實際上它的行爲與你所描述的略有不同,它均勻地分割菜單項之間的空間。如果你真的需要有相等的空間按鈕,你可以簡單地使用LinearLayout,但是你不能使用菜單結構。或者你必須實現或重寫ActionMenuView(實際上只是一個LinearLayout) –