2

在 iOS 中,有一个 UIView 能够根据父 View 给出的约束来调整自身大小的概念。例如,如果父 View 自己放大,那么子 View 可能会扩大或缩小以适应可用空间。这是内置在平台中的,使开发变得微不足道。

所以,我的问题是我正在开发一个使用 ICS 中的 ActionBar 的智能手机应用程序。我在那里有一个自定义视图,我使用以下方法设置:

actionBar.setCustomView(R.layout.my_custom_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:orientation="vertical" >
<TextView
    android:id="@+id/title"
    style="@style/TextAppearance.Title.Theme"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:scrollHorizontally="true"
    android:singleLine="true"
    android:text="Title" />
<TextView
    android:id="@+id/subtitle"
    style="@style/TextAppearance.SubTitle.Theme"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:scrollHorizontally="true"
    android:singleLine="true"
    android:text="Subtitle" />
</LinearLayout>

现在,在 ICS 中,ActionBar 在方向更改期间会更改其高度。横向模式比纵向模式更窄。这导致我的“字幕”文本在横向模式下被截断,因为 ActionBar 的高度已经缩小,而我的自定义标题视图文本本身没有调整大小。

是否可以在不以编程方式更改方向的情况下调整文本大小?

我过头了

onConfigurationChanged()

所以我不能只拥有一个单独的横向和纵向自定义标题视图。

旁注:这提醒了我......我希望在 onConfigurationChanged() 中我们可以提供一个新的 xml 布局,基本上只是调整屏幕上视图的位置。纵向和 xml 布局当然必须包含相同的视图,但是会有不同的布局信息。这将使生活更轻松,并且比再次调用 onCreate() 更有效。

4

1 回答 1

2

您的 TextView 已更改其大小,但文本大小未更改。没有额外的努力就无法更改文本大小,但您有不同的选择:

  1. 您为纵向和横向模式定义不同的文本大小值,并且不要覆盖 onConfigurationChanged。这可以通过创建两个维度.xml 来完成。一个在 res/values-land 中,另一个在 res/values-port 中
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="font_size">16dp</dimen>
</resources>

并在您的布局中将该值分配给您的 TextView

<TextView 
    android:textSize="@dimen/font_size"
  1. 第二种选择是以编程方式计算字体大小。这已经包含在另一个问题中
于 2012-05-08T17:39:24.107 回答