【问题标题】:How to auto adjust font size in the action bar spinner based on orientation?如何根据方向自动调整操作栏微调器中的字体大小?
【发布时间】:2012-07-27 10:37:33
【问题描述】:

我想在操作栏中放置一个微调器,就像在 Gmail 应用中一样。所以我创建了以下布局。

  <?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>

然后使用以下代码将此微调器加载到标题栏:

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

它按预期工作,除了在横向模式下,文本不适合。在 Gmail 应用程序中,字体大小会减小。 有没有办法根据方向自动调整字体大小?

【问题讨论】:

  • 根据您的问题,您知道如何包装 selected 元素内容(我的意思是文本)吗?我试图重现 Actionbar Gmaps 和 Gmail 微调器样式,但我不能。我的微调器宽度似乎被“下拉项目”内容拉长。如果 drop 中的项目有一个太大的元素,那么微调器会被拉伸。有没有办法包装微调器选定的元素?

标签: android android-layout


【解决方案1】:

//你需要在values文件夹中使用dimens.xml

<!-- 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>

用 values-land 再创建一个文件夹

在那个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>

参考:转到您的/&lt;android-sdk&gt;/platforms/android-15/data/res,您可以在这里找到。

【讨论】:

    【解决方案2】:

    他们最好的方法是在 Activity 启动时知道设备的方向,并根据它使用布局。

    如果纵向 setContentView(R.layout.layout_with_bigfont);

    If lanscape setContentView(R.layout.layout_with_smallfont);

    否则,您需要单独使用 setTextSize(xdp) 动态设置该视图(在本例中为 TextView)的字体,而不是针对不同方向使用 2 个不同的布局

    【讨论】:

    • dimens.xml 只是将所有硬编码值存储在 xml 中,并将它们与引用名称一起使用。如果您在单个活动中硬编码值或在许多活动中使用不同的值,为什么有人需要维度资源?
    • 你说的是这个吗?...stackoverflow.com/questions/3885164/…
    猜你喜欢
    • 1970-01-01
    • 2012-11-11
    • 1970-01-01
    • 2013-08-28
    • 2011-04-04
    • 2017-03-26
    • 1970-01-01
    • 2018-11-01
    • 2023-03-17
    相关资源
    最近更新 更多