2016-01-04 152 views
-1

我已經嘗試了近兩天試圖找出如何使一個簡單的自定義工具欄佈局是這樣的:Android的工具欄佈局

我真的想不通,怎麼增加高度的工具欄或至少如何獲得該效果。你能給我一些提示嗎? 謝謝!

+0

即使你想在「工具欄」中添加一個「工具欄」,並在裏面添加「水平LinearLayout」。添加一個'Layout'和'ImageView'作爲孩子,並給佈局權重2和'ImageView'權重1 –

+0

嗨,在檢查下面的答案後,請告訴我問題是否仍然存在 – piotrek1543

回答

0

要增加工具欄的高度:

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="@dimen/HEIGHT_THAT_YOU_WANT"> 

由於Toolbar是任何你想要的開始和結束標記內的ViewGroup你可以做。只是認爲它是LinearLayout。使用幾個嵌套的佈局來實現你想要的,最後用閉標籤關閉它。

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

雖然它並不真正遵循材料設計,我推薦的東西。

試試這個僞代碼

<Toolbar> 
    <LinearLayout orientation="horizontal"> 
    <LinearLayout orientation="vertical"> 
     <EditText/> 
     <LinearLayout orientation="horizontal"> 
     <ImageView src="smallImage"/> 
     <ImageView src="smallImage"/> 
     <ImageView src="smallImage"/> 
     <ImageView src="smallImage"/> 
     </LinearLayout> 
    </LinearLayout> 
    <ImageView src="BigImage"/> 
    </LinearLayout> 
</Toolbar> 

然後,您需要設置寬度/高度,更好地服務於您的佈局。

0

感謝所有提示!我終於做到了!我只是發佈解決方案,以防萬一有人需要它。所有的

1)首先,我創建在res /佈局稱爲toolbar.xml一個文件,其中包含

<?xml version="1.0" encoding="utf-8"?> 
<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:elevation="4dp" 
    android:background="?attr/colorPrimaryDark"> 

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

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

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceMedium" 
       android:text="Medium Text" 
       android:id="@+id/textView5" /> 

      <LinearLayout 
       android:orientation="horizontal" 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent"> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textAppearance="?android:attr/textAppearanceSmall" 
        android:text="Small Text" 
        android:id="@+id/textView6" /> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textAppearance="?android:attr/textAppearanceSmall" 
        android:text="Small Text" 
        android:id="@+id/textView7" /> 
      </LinearLayout> 

     </LinearLayout> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent"> 
      <View 
       android:layout_width="0dp" 
       android:layout_height="0dp" 
       android:layout_weight="1"` 
       /> 
      <Button 
       style="?android:attr/buttonStyleSmall" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="New Button" 
       android:id="@+id/button2" /> 
     </LinearLayout> 

    </LinearLayout> 

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

或者

Screen

2)toolbar.xml添加到main_activity .xml,所以

<?xml version="1.0" encoding="utf-8"?> 
<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="com.tbr.qps3.MainActivity"> 

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

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"> 
     ... 
    </TableLayout> 

</RelativeLayout> 

3)現在只需膨脹在onCreate()我thod

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); // Attaching the layout to the toolbar object 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    getSupportActionBar().setDisplayShowHomeEnabled(true); 


} 

希望它有幫助!