【问题标题】:TextView pushing right-placed controls out of the screenTextView 将正确放置的控件推出屏幕
【发布时间】:2013-07-12 22:41:18
【问题描述】:

我想在左侧放置一个 TextView,在 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吗?

标签: android android-layout


【解决方案1】:

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>

【讨论】:

    【解决方案2】:

    使用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 的值。它对所有值产生相同的结果吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多