Android 相对子间距

Android 相对子间距,android,android-layout,android-relativelayout,Android,Android Layout,Android Relativelayout,您好,我知道使用RelativeLayout,我们可以使用layout\u-toRightOf,layout\u-toLeftOf,layout\u将一个视图放置在另一个视图的旁边 然而,我想知道我们如何增加一些间距/边距?如果我只是设置边距,它将导致视图根据父视图设置边距。知道如何解决这个问题吗?否,边距将适用于视图的邻居,而不是父视图,例如,如果将layout\u marginLeft=20dp设置为view1,则view1的左侧(假设为view2)将与view1具有20dp的边距,而不是与

您好,我知道使用
RelativeLayout
,我们可以使用
layout\u-toRightOf
layout\u-toLeftOf
layout\u将一个视图放置在另一个视图的旁边


然而,我想知道我们如何增加一些间距/边距?如果我只是设置边距,它将导致视图根据父视图设置边距。知道如何解决这个问题吗?

否,边距将适用于视图的邻居,而不是父视图,例如,如果将
layout\u marginLeft=20dp
设置为view1,则view1的左侧(假设为view2)将与view1具有20dp的边距,而不是与父视图。否,边距将适用于视图的邻居,而不是父视图,例如,如果将
布局\u marginLeft=20dp
设置为view1,则view1的左侧(假设为view2)与view1的边距为20dp,而与父视图的边距完全不同。

使用填充设置视图内的边距:
android:padding=“5dp”

android:paddingLeft=“5dp”

android:paddingRight=“5dp”

android:paddingBottom=“5dp”


android:paddingTop=“5dp”
使用填充设置视图内部的边距:
android:padding=“5dp”

android:paddingLeft=“5dp”

android:paddingRight=“5dp”

android:paddingBottom=“5dp”


android:paddingTop=“5dp”

也许将填充设置添加到您的RelativeLayout中可能会起作用

因此,基本上有两种方法可以设置视图的间距填充边距

考虑以下代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<TextView
    android:id="@+id/text1"
    android:background="#ff0000"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text 1" />
<Button
    android:id="@+id/button1"
    android:background="#00ff00"
    android:layout_toRightOf="@id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1"/>
</RelativeLayout>

输出

  • 填充:填充设置描述视图内部的区域 允许视图绘制其内容的位置
  • 例如:更改上述代码中的填充设置:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    
    <TextView
        android:id="@+id/text1"
        android:background="#ff0000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:text="Text 1" />
    <Button
        android:id="@+id/button1"
        android:background="#00ff00"
        android:layout_toRightOf="@id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:text="Button 1"/>
    </RelativeLayout>
    
    
    
    输出

    (您是否注意到,现在,内容是在距离视图边界20dp后绘制的)

  • 边距:另一方面,边距设置描述可以绘制视图本身的区域
  • 向上述代码中添加边距设置(并删除填充):

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    
    <TextView
        android:id="@+id/text1"
        android:background="#ff0000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="Text 1" />
    <Button
        android:id="@+id/button1"
        android:background="#00ff00"
        android:layout_toRightOf="@id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="Button 1"/>
    </RelativeLayout>
    
    
    
    输出

    如您所见,这次的视图是在其周围留出20 dp的距离后绘制的


    因此,两种间距技术,填充和边距,第一种对“视图内容”有效,第二种对“视图本身”有效。

    可能会在您的RelativeLayout中添加填充设置

    因此,基本上有两种方法可以设置视图的间距填充边距

    考虑以下代码:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    
    <TextView
        android:id="@+id/text1"
        android:background="#ff0000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text 1" />
    <Button
        android:id="@+id/button1"
        android:background="#00ff00"
        android:layout_toRightOf="@id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1"/>
    </RelativeLayout>
    
    
    
    输出

  • 填充:填充设置描述视图内部的区域 允许视图绘制其内容的位置
  • 例如:更改上述代码中的填充设置:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    
    <TextView
        android:id="@+id/text1"
        android:background="#ff0000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:text="Text 1" />
    <Button
        android:id="@+id/button1"
        android:background="#00ff00"
        android:layout_toRightOf="@id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:text="Button 1"/>
    </RelativeLayout>
    
    
    
    输出

    (您是否注意到,现在,内容是在距离视图边界20dp后绘制的)

  • 边距:另一方面,边距设置描述可以绘制视图本身的区域
  • 向上述代码中添加边距设置(并删除填充):

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    
    <TextView
        android:id="@+id/text1"
        android:background="#ff0000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="Text 1" />
    <Button
        android:id="@+id/button1"
        android:background="#00ff00"
        android:layout_toRightOf="@id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="Button 1"/>
    </RelativeLayout>
    
    
    
    输出

    如您所见,这次的视图是在其周围留出20 dp的距离后绘制的


    因此,两种间距技术,填充和边距,第一种适用于“视图内容”,第二种适用于“视图本身”。

    谢谢,在我的例子中,它不起作用的原因是因为我使用addRule方法以编程方式设置所有内容。我想当我没有附带的XML文件时,它就不起作用了。@JayVDiyk,是的,当遇到这种需要用java编程的情况时,我通常会编写XML布局来证明它可以起作用,然后将布局转换为java代码。谢谢您的解释!谢谢,在我的例子中它不起作用的原因是因为我使用addRule方法以编程方式设置所有。我想当我没有附带的XML文件时,它就不起作用了。@JayVDiyk,是的,当遇到这种需要用java编程的情况时,我通常会编写XML布局来证明它可以起作用,然后将布局转换为java代码。谢谢您的解释!