【问题标题】:How to set Android toolbar height?如何设置Android工具栏高度?
【发布时间】:2015-06-26 22:02:40
【问题描述】:

我想实现这个:

我的想法是制作一个高度更大的自定义工具栏,并正常使用 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>

以及Activity中的这个函数:

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);
    }

【问题讨论】:

  • 您使用的是ActionBar。你的Toolbar 代码在哪里?您可以在您的 XML 中设置高度 android:minHeight="?attr/actionBarSize"。使用高度而不是 minHeight。

标签: android android-actionbar android-tabhost android-toolbar


【解决方案1】:
<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 将它的高度包裹在它的孩子周围,这意味着只有在测量孩子时它才会得到一个固定的大小。你的最小高度在5060 dip 左右,这就是你的ToolBar 的高度。 所以如果你孩子的身高加起来没有达到一个合理的大数字,它仍然是&lt;= 50

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

【讨论】:

    【解决方案2】:

    如果您不需要操作栏的功能(如Theme.Holo.NoActionBar),请为此活动或整个应用使用没有操作栏的主题。 您可以直接在活动根布局中添加顶部栏的布局。如果您将在其他活动中使用它,请考虑为布局甚至您自己的 ActionBarView 类制作单独的 xml 文件。

    【讨论】:

      【解决方案3】:

      你可以像这样使用 AppBarLayout:

      
      <android.support.design.widget.AppBarLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:theme="@style/AppTheme">
      
              <android.support.v7.widget.Toolbar
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:id="@+id/toolbar_"
                  app:layout_scrollFlags="scroll|enterAlways"
                  app:popupTheme="@style/AppTheme.PopupOverlay">
      
              </android.support.v7.widget.Toolbar>
      
              <android.support.design.widget.TabLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:id="@+id/tabs"
                  app:tabBackground="@color/White"
                  app:tabIndicatorColor="@color/OrangeFFC200"
                  app:tabTextAppearance="@style/MineCustomTabText">
      
              </android.support.design.widget.TabLayout>
      
      </android.support.design.widget.AppBarLayout>
      
      

      【讨论】:

        猜你喜欢
        • 2016-06-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-31
        • 2017-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多