【问题标题】:Android: Padding not working in RelativeLayoutAndroid:填充在RelativeLayout中不起作用
【发布时间】:2016-04-21 22:26:45
【问题描述】:

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

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

【问题讨论】:

    标签: android padding android-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>
    

    【讨论】:

    • 太棒了。当您将“dp”单位添加到您的 layout_widths 时,它是完美的。
    • 请解释您所做的更改以及解决问题的原因。它只是改变了 TextView 和 Switch 的顺序吗?为什么 TextView 中的 layout_width '0' 有意义?
    【解决方案2】:

    您需要告诉RelativeLayout 您的TextView 留给您的Switch。 您还需要将您的 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>
    

    【讨论】:

      猜你喜欢
      • 2012-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-06
      • 2018-01-19
      • 1970-01-01
      相关资源
      最近更新 更多