【问题标题】:Expanding TextView with screen size up to maxWidth将屏幕尺寸扩大到 maxWidth 的 TextView
【发布时间】:2014-03-09 02:02:12
【问题描述】:

此问题与使用屏幕宽度展开的元素有关,但最多只能达到 maxWidth。

我的场景涉及一个垂直方向的LinearLayout,其中包含固定的行序列。这些行中的每一行都是一个水平线性布局,包含一些EditText 元素,由TextView 元素分隔。 TextView 元素的文本包含一个数学符号(例如 + 或 - 或 /),旨在指示第二个框的内容将添加到第一个框(或从第一个框减去等),以便在 UI 的其他地方显示。

EditText 元素必须保持其当前的固定宽度,但我希望TextView 元素(充当分隔符)在屏幕允许的情况下扩大宽度,但最大宽度为 30dp。目前,一个示例行的布局如下所示:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingLeft="@dimen/row_side_padding"
    android:paddingRight="@dimen/row_side_padding"
    android:paddingTop="5dp"
    android:paddingBottom="5dp">

    <EditText
        android:id="@+id/editTextBoxOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/box_one_hint"
        android:imeOptions="actionDone"
        android:inputType="numberSigned"
        android:maxLines="1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/plus" />

    <EditText
        android:id="@+id/editTextBoxTwo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/box_two_hint"
        android:imeOptions="actionDone"
        android:inputType="numberSigned"
        android:maxLines="1" />
</LinearLayout>

当前代码将 TextView 的宽度换成文本的宽度(在本例中只是一个加号)。这正是我想要的小屏幕!

但是,在更宽的屏幕上,如果 TextView 变宽以将元素隔开一点,那就太好了。我可以将 TextView 设置为:

<TextView
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@string/plus" />

但这会使 TextView 太大。如果屏幕宽度允许,我希望它最大扩展为 30dp。但是from what I understandandroid:maxWidth 不适用于android:layout_width="match_parent",所以任何 maxWidth 都会被忽略。

那么如何根据屏幕宽度扩展我的 TextView,但只能扩展到最大宽度,同时让我的 EditText 元素保持相同大小?

【问题讨论】:

    标签: android android-layout textview


    【解决方案1】:

    android:layout_width 设置为0dip...这将有助于TextView 占用EditTexts 大小之后的可用空间。

    <TextView
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/plus" />
    

    【讨论】:

      【解决方案2】:

      对于那些正在解决同样问题的人......

      似乎没有办法做到这一点。相反,最好的方法是创建一些与某些屏幕宽度相关联的独立布局文件(即在 res/layout-w600dp 中以及在 res/layout 本身中以捕获最小的屏幕宽度)。我在 layout_plus.xml 文件中指定了一个 TextView:

      <TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:paddingTop="10dp"
          android:paddingLeft="5dp"
          android:paddingRight="5dp"
          android:gravity="center"
          android:text="@string/plus" >
      </TextView>
      

      对于这些与屏幕宽度相关的布局,我使用了不同数量的 paddingLeft 和 paddingRight,以手动执行 TextView 周围空间的扩展。如果你愿意,你可以改变 TextView 的宽度。

      然后在您的主布局中,删除您尝试扩展的 TextView 并使用 tag。这将允许您的屏幕宽度相关文件自动拖入 - 无论哪个合适。我的包含看起来像这样:

      <include
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           layout="@layout/layout_plus"/>
      

      文档声明您应该覆盖 layout_width 和 layout_height 属性。

      我也想覆盖其他属性,例如覆盖文本以包含减号而不是加号(允许我更多地重用)。不幸的是,include 不允许您覆盖其他属性,因此我不得不为此创建一组 layout_minus.xml 布局。

      作为使用修改后的布局的替代方法,可以选择简单地创建由 TextView 显示的字符串资源的屏幕宽度相关版本,您可以在其中插入用于较大宽度版本的空格。这将需要更少的更改,并且可能需要更少的新文件,但是字符串中的硬编码空间使得以后更难调整和调整。

      【讨论】:

        猜你喜欢
        • 2017-05-05
        • 1970-01-01
        • 2012-05-31
        • 1970-01-01
        • 2012-05-24
        • 1970-01-01
        • 1970-01-01
        • 2012-04-10
        • 1970-01-01
        相关资源
        最近更新 更多