TextView将右侧控件推出屏幕外

3
我想在屏幕左侧添加一个TextView,右侧放置一个控件(例如CheckBox)。我希望该控件在屏幕上左对齐。这可以通过LinearLayout或RelativeLayout很容易实现。例如,我可以使用LinearLayout实现:
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <TextView
        android:id="@+id/todo_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="@android:style/TextAppearance.Small"
         android:maxLines="2"
        android:textStyle="bold" />
    <View
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1" />
    <CheckBox
        android:id="@+id/todo_checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:enabled="false"
        android:focusable="false"/>
 </LinearLayout>

问题在于当TextView的文本太长时,它会将复选框推出屏幕,使复选框不再可见。 相反,我希望复选框固定在屏幕的右端,并且如果需要,TextView最终会分为两行。 如何实现这一点?

你试过用layout_weightweightsum来布局吗? - chancea
2个回答

7

使用android:layout_weight="1",文本视图和复选框将始终位于右侧。请查看:Android Layout Weight

<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal" >
   <TextView
      android:id="@+id/todo_title"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:textAppearance="@android:style/TextAppearance.Small"
      android:maxLines="2"
      android:layout_weight="1"
      android:textStyle="bold" />
  <CheckBox
      android:id="@+id/todo_checkbox"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:enabled="false"
      android:focusable="false"/>
</LinearLayout>

0

使用weightsumlayout_weight

weightsum是赋给父级的值,表示所有子组件必须加起来等于该值。

layout_weight赋给该布局的子元素。该值对应于组件将占用的布局量。

<LinearLayout
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal"
   android:weightsum = "100" >
   <TextView
      android:id="@+id/todo_title"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:textAppearance="@android:style/TextAppearance.Small"
      android:maxLines="2"
      android:layout_weight="30"
      android:textStyle="bold" />
  <CheckBox
      android:id="@+id/todo_checkbox"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:enabled="false"
      android:layout_weight="70"
      android:focusable="false"/>

</LinearLayout>

请记住,这些值是反向的(不确定它是如何工作的),但最大的layout_weight值占用最小的区域。

尝试调整layout_weight的值。所有值都会产生相同的结果吗? - Alexey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接