android - 类型 "<fill_parent><wrap_content>"的布局

标签 android layout

不确定这个问题标题是否清楚我的意思,所以这里有详细信息。

我想要实现的是简单的布局,其中两个 TextView 控件依次水平对齐。第一个 TextView 可能包含几行文本,第二个 TextView 将只包含一个数字。

第二个 TextView 的宽度应始终与显示其内容所需的一样多,并向右对齐,高度应等于第一个 TextView 的高度,以便我可以垂直居中显示数据。第一个 TextView 应占用剩余空间(用于宽度)并根据需要垂直拉伸(stretch)。

示例 1:

  ---------------------------------
  |Small Text.                 123|
  ---------------------------------

示例 2:

  ---------------------------------
  |Long Text starts here ....     |
  |... continues here .......  123|
  |... and finishes here ....     |
  ---------------------------------

水平方向的 LinearLayout 在这里不好,因为它从左到右放置控件。在这种情况下,我需要先将 wrap_content 设置为第二个 TextView,然后将 fill_parent 设置为第一个 TextView。

使用 RelativeLayout 我也无法获得描述的布局。

我不能在这里使用 layout_weight 方法,因为我不知道第二个 TextView 中的数据长度:在一种情况下它可能是 1 个字符,在另一种情况下 - 8 个字符。所以在 1 个字符的情况下,我将有未使用的空间。

那么,我有机会构建这样的布局吗?

最佳答案

像下面这样的布局应该可以解决问题:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:singleLine="false"
        android:text="TextView" />
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center_vertical|right"
        android:text="TextView" />
</LinearLayout>

关于android - 类型 "<fill_parent><wrap_content>"的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8326114/

相关文章:

user-interface - 按一个因子调整动态大小的 Widget

android - 使用标志 -Dagger.hilt.disableModulesHaveInstallInCheck=true 从 Dagger 迁移到 Hilt 时抑制 @InstallIn 检查时出错

android - 更新到 Android studio 3.5 后出现以下错误 - FAILURE : Build failed with an exception

android - linearlayout 中的 scrollview 强制按钮离开屏幕

android - 如何在 Activity 启动后立即确定 View 大小

css - 将 sub-div 的宽度扩展到 100%

android - requestLocationUpdates 从未调用过

java - 获取 xml 文件的内容并将内容存储到字符串中以便解析内容

android - 如何以原始比例设置自定义单选按钮android的背景图像?

java - 尝试创建多个 JLabel,但只出现一个