2016-01-16 102 views
-1

我想要在描述文本和交換機之間填充填充,但paddingRight屬性不起作用。爲什麼?Android:填充不能在RelativeLayout中工作

enter image description here

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/layoutLayout" 
    android:layout_below="@+id/layoutHeader" 
    android:layout_marginTop="15dp" 
    android:layout_marginLeft="15dp" 
    android:layout_marginRight="15dp"> 

    <Switch 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/widgetShortSwitch" 
     android:layout_centerVertical="true" 
     android:layout_alignParentRight="true" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="This is a long description text that should not overlay." 
     android:textColor="#000000" 
     android:paddingRight="15dp" 
     android:id="@+id/widgetShortTextView" 
     android:layout_centerVertical="true" 
     android:layout_alignParentLeft="true" 
     /> 
</RelativeLayout> 

回答

1

嘗試......

<?xml version="1.0" encoding="utf-8"?> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/layoutLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/layoutHeader" 
     android:layout_marginLeft="15dp" 
     android:layout_marginRight="15dp" 
     android:layout_marginTop="15dp" 
     android:weightSum="1"> 

     <TextView 
      android:id="@+id/widgetShortTextView" 
      android:layout_width="0" 
      android:layout_height="wrap_content" 
      android:paddingBottom="5dp" 
      android:layout_weight="0.85" 
      android:paddingRight="15dp" 
      android:text="This is a long description text that should not overlay." 
      android:textColor="#000000" /> 

     <Switch 
      android:id="@+id/widgetShortSwitch" 
      android:layout_width="0" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      android:layout_weight="0.15" /> 
    </LinearLayout> 
+0

太棒了。當您將「dp」單位添加到layout_widths時,它是完美的。 – Kewitschka

0

你需要告訴RelativeLayout的,你的TextView是留給你的交換機。 您還需要將您的TextView作爲alignParentLeft,以便它將從左側開始。

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/layoutLayout" 
    android:layout_below="@+id/layoutHeader" 
    android:layout_marginTop="15dp" 
    android:layout_marginLeft="15dp" 
    android:layout_marginRight="15dp"> 

    <Switch 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/widgetShortSwitch" 
     android:layout_centerVertical="true" 
     android:layout_alignParentRight="true" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="This is a long description text that should not overlay." 
     android:textColor="#000000" 
     android:paddingRight="15dp" 
     android:id="@+id/widgetShortTextView" 
     android:layout_centerVertical="true" 
     android:layout_alignParentLeft="true" 
     android:layout_toLeftOf="@id/widgetShortSwitch" 
     android:layout_alignParentLeft="true" 
     /> 
</RelativeLayout>