簡體   English   中英

如何在工具欄中心獲取標題

[英]How to get title at center of Toolbar

我正在嘗試將“折疊的工具欄”的標題居中。 但是基本布局本身似乎存在一些錯誤。 標題由於某些原因而有偏移。 無法找出解決方法。 同樣,似乎也沒有屬性將負的邊距/填充應用於collapsedTitle。

<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/app_bar_height"
    android:fitsSystemWindows="true"
    android:theme="@style/XXXXAppTheme.AppBarOverlay">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsingToolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:collapsedTitleGravity="center"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleGravity="top|center_horizontal"
        app:expandedTitleMarginTop="-10dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:title="REGISTER">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/XXXXAppTheme.PopupOverlay"/>

        <ImageView
            android:id="@+id/ivContent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/white_background"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:layout_collapseMode="parallax"/>

    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <FrameLayout
        android:id="@+id/flContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

CollapsedToolbar

在“ Toolbar ”中嘗試此操作

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_top"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/color"
    app:theme="@style/ToolBarTheme" >


     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Register"
         android:layout_gravity="center"
         android:id="@+id/toolbar_title" />


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

要使其居中,請按照以下步驟操作:

要在Toolbar使用自定義標題,您所要做的就是記住, Toolbar只是一個精美的ViewGroup,因此您可以像這樣添加自定義標題:

  <android.support.v7.widget.Toolbar android:id="@+id/toolbar_top" android:layout_height="wrap_content" android:layout_width="match_parent" android:minHeight="?attr/actionBarSize" android:background="@color/action_bar_bkgnd" app:theme="@style/ToolBarTheme" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Toolbar Title" android:layout_gravity="center" android:id="@+id/toolbar_title" /> </android.support.v7.widget.Toolbar> 

這意味着您可以根據需要設置TextView樣式,因為它只是常規的TextView 因此,在您的活動中,您可以像這樣訪問標題:

 Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top); TextView mTitle = (TextView) toolbarTop.findViewById(R.id.toolbar_title); 

來自: Android工具欄中心標題和自定義字體

要刪除默認的應用名稱標題,請執行以下操作:

setSupportActionBar(toolbar_top); 
getSupportActionBar().setDisplayShowTitleEnabled(false);

希望對你有幫助

您應該使用此。 在側面Toolbar添加TextView

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

    <android.support.v7.widget.AppCompatTextView
        android:id="@+id/toolbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:layout_gravity="center"/>

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM