7

我要实现这一点:如何设置Android工具栏高度?

我的想法是做一个自定义工具栏有一个更大的高度,并与tabhost和tabpager正常工作。我已经实现了它,但工具栏显示的是正常的高度,所以它不会显示为我想要的,只是顶部。这是正确的方法还是可以将TabHost设置在线性/相对布局下?因为我不需要将它作为一个操作栏来使用。

相关代码

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/toolbarTitle" 
    android:orientation="vertical" 
    android:background="@color/black" 
    android:layout_width="match_parent" 
    android:fitsSystemWindows="true" 
    android:minHeight="?attr/actionBarSize" 
    android:theme="@style/AppTheme" 
    android:layout_height="wrap_content"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
    <ImageView 
     android:id="@+id/img_logo" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="55dp" 
     android:scaleType="centerInside" 
     android:src="@drawable/logo_home" 
     /> 

    <TextView 
     android:id="@+id/txt_version" 
     android:text="@string/app_version" 
     android:textColor="@color/white" 
     android:layout_below="@+id/img_logo" 
     android:paddingBottom="10dp" 
     android:paddingTop="15dp" 
     android:gravity="center" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 
    </RelativeLayout> 
</android.support.v7.widget.Toolbar> 

而且在活动这个功能:

private void setupActionBar() 
    { 
     ActionBar ab = getSupportActionBar(); 
     ab.setDisplayShowCustomEnabled(true); 
     ab.setDisplayShowTitleEnabled(false); 
     LayoutInflater inflator = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View v = inflator.inflate(R.layout.toolbar_title, null); 
     ab.setCustomView(v); 
    } 
+0

您正在使用'ActionBar'。 “工具栏”代码在哪里?你可以在你的XML中设置高度'android:minHeight =“?attr/actionBarSize”'。使用高度而不是minHeight。 –

回答

11
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/toolbarTitle" 
android:orientation="vertical" 
android:background="@color/black" 
android:layout_width="match_parent" 
android:fitsSystemWindows="true" 
android:minHeight="?attr/actionBarSize" //pay attention here 
android:theme="@style/AppTheme" 
android:layout_height="wrap_content"> //pay attention here 

ToolBar这是一个ViewGroup被包木窗的高度围绕其孩子这意味着将获得只有当孩子们测量的固定大小。你的最低身高大约是5060dip,这就是你的ToolBar会有多低。 因此,如果您的孩子身高加起来还不到一个合理的大数目它仍然是<= 50

给它一个首选高度android:layout_height="200dp"

0

使用一些主题与本次活动或不采取行动吧整个应用程序,如果你不需要操作栏的功能(如Theme.Holo.NoActionBar)。 您可以直接在活动根布局中添加顶部栏的布局。如果您将在其他活动中使用它,请考虑为布局甚至您自己的ActionBarView类创建单独的xml文件。