44

サポート ツールバーの [戻る] ボタンを中央に配置しようとすると問題が発生します。ActionBarActivity 内で使用しています。

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:toolbar="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    toolbar:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    toolbar:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

そして、Activity 内のナビゲーションを次のonCreate()ように設定します。

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

getSupportActionBar().setTitle(R.string.title_activity_scanner);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

しかし、私が得ているのはこれです:

ご覧のとおり、戻るボタンの位置が間違っています

編集: 問題は 40 dp に設定するためのカスタム値にあるようです?attr/actionBarSizeが、代わりにタイトルが間違っていることがわかりました。

4

9 に答える 9

2

ActionBarActivity 内

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  setSupportActionBar(toolbar);
  getSupportActionBat().setTitle("Hello World"); 
  getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  getSupportActionBar().setHomeButtonEnabled(true);

レイアウトコード:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/primary"
        app:theme="@style/ToolbarTheme"
        app:popupTheme="@style/Theme.AppCompat"/>

そしてスタイル:

 <style name="AppTheme.Base" parent="Theme.AppCompat.Light">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primaryDark</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="windowActionBar">false</item>
</style>

<style name="AppTheme" parent="AppTheme.Base">

ツールバーは単なるビューグループであるため、好きなようにスタイルを設定できることに注意してください。ここに画像リンクがあります:ツールバーのサンプル

それが役に立てば幸い。

于 2015-04-27T09:35:14.660 に答える
1

これを実現する最善の方法は、ツールバーから通常の [戻る] ボタンを削除し、XML レイアウトにカスタム ImageButton を追加して、それに画像を設定することです。この ImageButton は、ツールバーの好きな場所に配置できます。

アクティビティ レイアウト コードは次のようになります。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

    <ImageButton
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:id="@+id/button"
        android:src="@drawable/attach_icon"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>
于 2015-04-27T09:17:50.497 に答える
0

私にとっては、どの解決策もうまくいきませんでした。私の場合、問題は私が適用したマージンでした:

 <android.support.v7.widget.Toolbar
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     ... >

     <View
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp" />

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

ツールバーに直接適用した後、ボタンは垂直方向に中央に配置されました。

 <android.support.v7.widget.Toolbar
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_marginBottom="10dp"
     android:layout_marginTop="10dp"
     ... >

     <View
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

 </android.support.v7.widget.Toolbar>
于 2016-04-23T15:23:54.463 に答える
0

@Rick Sanchezの回答に便乗- ツールバーのminHeight属性のサイズがわかっている場合は、次を使用できます。

android:gravity="top"
app:titleMarginTop="@dimen/margin_to_center"

ツールバーでテキストを中央揃えにします。使用している場合android:minHeight="?actionBarSize"、これは約13pになります

于 2015-09-28T19:59:56.637 に答える