2014-09-20 32 views
2

我有我的应用程序的某些部分的相对布局,由一个复选框,一个按钮,一些文本和另一个按钮组成(请参见第一个截图,例如:[5分钟]屏幕关闭后[除外])。相对布局中的按钮被TextView推下屏幕

问题在于其他语言,最后一个按钮左侧的文本可能会更长,推动“除外”按钮稍微或完全脱离屏幕。我还能怎么做,以便按钮(连同文本)绕回到下一行?现在只有文字。下面是代码:

 <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> 

Normal layout "except" button pushed off screen http://i60.tinypic.com/214zj3t.png

回答

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每个"0dp"的,因为你使用weight的水平布局时,通常都需要这一点。我还将android:layout_centerVertical="true"替换为android:layout_gravity="center_vertical"以匹配LinearLayout的相应属性。我还删除了to_right_of以及LinearLayout不再需要的属性。

我还没有测试过,但这应该让你关闭。

LinearLayout docs

另一个选择可能是使用的TextViewmaxWidth财产。

+0

感谢您的建议@codeMagic。它并不完全符合我想要的,即将文本AND按钮推到下一行,但它确实有效。 textView成为多行,按钮保持原位。我不得不使按钮的重量0.00000001和textView 1的重量,否则按钮被挤压。我不确定为什么权重看起来落后。我会稍等一下,看看是否有人可以想出一种方法来处理文本,最后一个按钮将换行到下一行,否则我会将其标记为正确。 – Flyview 2014-09-20 13:06:28

+1

其实更好的是,我将内部textView的权重设置为1,并且不对按钮设置权重(默认值为0),但在按钮上使用wrap_content作为宽度,并且它完美地工作! – Flyview 2015-04-17 17:12:23