Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
426 views
in Technique[技术] by (71.8m points)

android - How to auto adjust font size in the action bar spinner based on orientation?

I wanted to place a spinner in the action bar, just like in the Gmail app. So I created the following layout.

  <?xml version="1.0" encoding="utf-8" ?> 
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
  <TextView android:id="@+id/spinner_list_item_selected_header" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_gravity="center" 
android:textStyle="bold" 
android:gravity="center" 
android:text="Sales" /> 
      <TextView android:id="@+id/spinner_list_item_selected_text"  android:layout_width="match_parent"
android:layout_height="wrap_content" 
android:layout_gravity="center" 
android:textStyle="bold" 
android:gravity="center" 
android:text="" /> 
      </LinearLayout>

This spinner is then loaded on to the title bar using the following code:

getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getActionBar().setListNavigationCallbacks(mSpinnerAdapter, mOnNavigationListener);

It works as expected, except that in the landscape mode, the text are not fitting in. In Gmail app, the font size gets reduced. Is there a way to auto adjust the font size based on the orientation?

Gmail Portrait

Gmail Landscape

My Portrait

My Landscape

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

//you need to use dimens.xml in values folder

<!-- Text size for action bar titles -->
    <dimen name="action_bar_title_text_size">18dp</dimen>
    <!-- Text size for action bar subtitles -->
    <dimen name="action_bar_subtitle_text_size">14dp</dimen>
    <!-- Top margin for action bar subtitles -->
    <dimen name="action_bar_subtitle_top_margin">-3dp</dimen>
    <!-- Bottom margin for action bar subtitles -->
    <dimen name="action_bar_subtitle_bottom_margin">5dip</dimen>

create one more folder with values-land

in that dimens.xml

 <!-- Text size for action bar titles -->
    <dimen name="action_bar_title_text_size">16dp</dimen>
    <!-- Text size for action bar subtitles -->
    <dimen name="action_bar_subtitle_text_size">12dp</dimen>
    <!-- Top margin for action bar subtitles -->
    <dimen name="action_bar_subtitle_top_margin">-2dp</dimen>
    <!-- Bottom margin for action bar subtitles -->
    <dimen name="action_bar_subtitle_bottom_margin">4dip</dimen>

Ref: go to your /<android-sdk>/platforms/android-15/data/res here you can found this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...