我的应用程序的某些部分有一个相对布局,由复选框,按钮,一些文本和另一个按钮组成(请参见第一个屏幕截图,例如:屏幕关闭后[5分钟] [除外])。
问题出在其他语言上,最后一个按钮左边的文字可能会长得多,推动"除了"按钮略微或完全脱离屏幕。我怎么能这样做,以便按钮(连同文本)环绕下一行?现在只有文本。这是代码:
<RelativeLayout
android:id="@+id/layout_after_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp">
<Button
android:id="@+id/button_set_time_turn_off"
android:layout_width="wrap_content"
android:layout_height="35sp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
style="?android:attr/buttonStyleSmall"
android:layout_centerVertical="true"
android:textSize="16sp"/>
<TextView
android:id="@+id/textView_data_check_minutes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginStart="3dp"
android:layout_toRightOf="@+id/button_set_time_turn_off"
android:layout_toEndOf="@+id/button_set_time_turn_off"
android:layout_centerVertical="true"
android:textSize="16sp"
android:textColor="@android:color/primary_text_dark"
android:text="@string/text_disable_after_time" />
<Button
android:id="@+id/button_set_disable_exceptions"
android:layout_width="wrap_content"
android:layout_height="35sp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:layout_toRightOf="@+id/textView_data_check_minutes"
android:layout_toEndOf="@+id/textView_data_check_minutes"
style="?android:attr/buttonStyleSmall"
android:layout_centerVertical="true"
android:text="@string/button_disable_except"
android:textSize="16sp"/>
</RelativeLayout>
"except" button pushed off screen http://i60.tinypic.com/214zj3t.png
答案 0 :(得分:0)
您可以将它们包装在LinearLayout
中,因为毕竟它们只是线性的。然后根据您的需要定位LinearLayout
您需要的方式。
这样您就可以使用layout_weight
来确保每个View
只占用所需的空间。你需要使用weights
来获得你想要的东西,但像1 | 2 | 1这样的东西看起来很接近你想要的东西。
<LinearLayout
android:id="@+id/layout_after_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp">
<Button
android:id="@+id/button_set_time_turn_off"
android:layout_width="0dp"
android:layout_height="35sp"
android:layout_weight="1"
android:paddingTop="0dp"
android:paddingBottom="0dp"
style="?android:attr/buttonStyleSmall"
android:layout_gravity="center_vertical"
android:textSize="16sp"/>
<TextView
android:id="@+id/textView_data_check_minutes"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_marginLeft="3dp"
android:layout_marginStart="3dp"
android:layout_gravity="center_vertical"
android:textSize="16sp"
android:textColor="@android:color/primary_text_dark"
android:text="@string/text_disable_after_time" />
<Button
android:id="@+id/button_set_disable_exceptions"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="35sp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
style="?android:attr/buttonStyleSmall"
android:layout_gravity="center_vertical"
android:text="@string/button_disable_except"
android:textSize="16sp"/>
</LinearLayout>
请注意,我创建了每个width
的{{1}},因为在将"0dp"
用于水平布局时通常需要这样做。我还将weight
替换为android:layout_centerVertical="true"
以匹配android:layout_gravity="center_vertical"
的相应属性。我还删除了LinearLayout
以及to_right_of
不再需要的属性。
我还没有对它进行测试,但这应该会让你接近。
另一种选择可能是使用LinearLayout
的{{3}}属性。