ActionBarのタイトルをカスタマイズ

ActionBarのアプリアイコンの横のタイトルをTextViewに置き換えてクリックイベント処理を行う。

タイトル用Viewの設定(layout/title.xml)

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

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:textSize="15sp"
        android:textColor="#ffffff"
        android:maxLines="1"
        />

</LinearLayout>

 

Activity

ActionBar actionBar = getActionBar();
//カスタム許可
actionBar.setDisplayShowCustomEnabled(true);
//通常のタイトル非表示
actionBar.setDisplayShowTitleEnabled(false);
LayoutInflater inflater = LayoutInflater.from(this);
//タイトル用View
View v = inflater.inflate(R.layout.title, null);
TextView tvTitle = (TextView)v.findViewById(R.id.title);
tvTitle.setOnClickListener(this);
actionBar.setCustomView(v);
tvTitle.setText("Title");
//クリックイベントは普通のViewと同じようにonClickで拾う

 

 

参考

カテゴリー: Android, View タグ: パーマリンク

コメントを残す

メールアドレスが公開されることはありません。 ※ が付いている欄は必須項目です