4

タイトルをアクションバーの中央に配置し、その横にアクションアイコンを配置したいと思います(重力設定ではそれらを無視します)。

これが欲しい: ここに画像の説明を入力

代わりに私はこれを持っています: ここに画像の説明を入力

これが私のコードです:

    ActionBar actionBar = getSupportActionBar();

    ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);

    actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.top_bar));
    actionBar.setCustomView(LayoutInflater.from(this).inflate(R.layout.actionbar_layout, null), params); 
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayShowTitleEnabled(false);

私のレイアウト:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:padding="8dp"
        android:src="@drawable/launcher_icon" />

    <TextView
        android:id="@+id/actionbar_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:maxHeight="1dp"
        android:maxLines="1"
        android:paddingLeft="48dp"
        android:paddingRight="48dp"
        android:text="Bacon ipsum"
        android:textColor="#777777"
        android:textSize="20sp" />

</RelativeLayout>

だから私の問題は、タイトルを中央に配置することだけが機能することです.0のアクションバーアイテムが隣にある場合、アイテムを追加するとすぐに中央揃えが台無しになります。それと一緒にアフォーダンスアップボタンを使用できれば、それも素晴らしいでしょう(これはまた、今ではセンタリングを台無しにします).

どんな助けでも大歓迎です。

独自のアクションバー アイテム/ボタン ロジックを実装したくありません。

4

3 に答える 3

3

TextView でこれを試すことができます。

<TextView
    android:id="@+id/actionbar_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:maxHeight="1dp"
    android:maxLines="1"
    android:paddingLeft="48dp"
    android:paddingRight="48dp"
    android:text="Bacon ipsum"
    android:textColor="#777777"
    android:textSize="20sp" />
于 2013-05-13T14:12:39.803 に答える
2
  1. RelativeLayout からランチャー アイコンを削除します。
  2. RelativeLayout の幅をwrap_content
  3. 削除する:

    actionBar.setDisplayShowCustomEnabled(true); actionBar.setDisplayShowHomeEnabled(false); actionBar.setDisplayShowTitleEnabled(false);

  4. 追加:

    ActionBar.LayoutParams lp = new ActionBar.LayoutParams(
    
    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER);
    
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM)
    actionBar.setCustomView(yourInflatedCustomView, lp);
    
于 2014-03-07T15:27:40.420 に答える