2013-07-12 98 views
3

我想在左侧放置一个TextView,并在TextView的右侧放置一个控件(例如CheckBox)。我希望控件在屏幕上左对齐。这并不难通过LinearLayout或RelativeLayout获得。比如我这样做与LinearLayout中:TextView将右侧控件推出屏幕

<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最终会分成两行。 我该如何做到这一点?

+0

你有没有试过玩'layout_weight'和'weightsum'? – chancea

回答

7

使用android:layout_weight="1"为textview和复选框将始终在右侧。检查: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值占据最小的面积。

+0

不适合我。 CheckBox仅部分显示。 – user1781028

+0

玩弄layout_weight的值。它是否对所有值产生相同的结果? – Alexey