Android:填充在RelativeLayout中不起作用

Android:填充在RelativeLayout中不起作用,android,padding,android-relativelayout,Android,Padding,Android Relativelayout,我想在描述文本和开关之间添加一个填充,但paddingRight属性不起作用。为什么? 试试这个 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layoutLayout" android:layout_width="matc

我想在描述文本和开关之间添加一个填充,但paddingRight属性不起作用。为什么?


试试这个

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

您需要告诉RelativeLayout,您的文本视图由您自己来切换。 您还需要将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>

太棒了。当你将“dp”单位添加到你的布局宽度时,它是完美的。请解释你改变了什么以及为什么它纠正了问题。它只是改变了文本视图和开关的顺序吗?为什么文本视图中的布局宽度“0”有意义?
<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>