やりたかったことは、タイトルバーに右寄せで選択中のアカウントを表示する。選択中ラベルと重なる時は、選択中ラベルは省略せずにそのまま表示して、アカウントの方は最後を省略(...)で表示する。
できた結果がこれ↓
意外に苦労したのでソースを公開
custom_title.xmlを作成して、
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/title_left_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/title_left_text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:ellipsize="none"
android:singleLine="true"
style="?android:attr/windowTitleStyle"
android:gravity="center_vertical"
/>
<TextView
android:id="@+id/title_right_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:ellipsize="end"
android:singleLine="true"
android:textColor="#ffffff"
android:textStyle="italic"
android:gravity="center_vertical"
/>
</LinearLayout>
</RelativeLayout>
ActivityのonCreateで、
public class GmailResenderActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); // タイトルバーをカスタム宣言
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title); // カスタムタイトル登録
タイトルバーを更新したいところで、// タイトルバー表示 ImageView iv = (ImageView) findViewById(R.id.title_left_image); // 左側イメージアイコン iv.setImageResource(R.drawable.icon); TextView tv; tv = (TextView) findViewById(R.id.title_left_text); // 左側タイトル tv.setText(selectedLabel); // ラベル名表示 tv = (TextView) findViewById(R.id.title_right_text); // 右側タイトル SpannableStringBuilder spannable = new SpannableStringBuilder(); spannable.append(account); // 右揃えの SPAN インスタンスを生成。 AlignmentSpan.Standard right_span = new AlignmentSpan.Standard(Layout.Alignment.ALIGN_OPPOSITE); // SPAN を SpannableStringBuilder に組み込む。 spannable.setSpan(right_span, 0, spannable.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); tv.setText(spannable);で出来上がり。


0 件のコメント:
コメントを投稿