2016-01-04 264 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); 


} 

希望它有帮助!