RelativeLayout 子间距
RelativeLayout Interchild Margin
您好,我知道使用 RelativeLayout
,我们可以使用 layout_toRightOf
、layout_toLeftOf
、layout_above
.
将一个视图放在另一个视图旁边
但是,我想知道我们如何为其添加一些间距/边距?如果我简单地设置边距,它会导致视图相对于父视图设置边距。知道如何解决这个问题吗?
不,边距将适用于视图的邻居,而不是 parents,例如,如果您将 layout_marginLeft=20dp
设置为视图 1,则视图 1 的左侧,假设视图 2,将有 20dp 的边距视图 1,根本没有 parents。
使用内边距设置视图内的边距:
android:padding="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:paddingTop="5dp"
也许向 RelativeLayout 添加 Padding 设置可能会奏效。
所以基本上有两种方法可以设置视图的间距。 边距 和边距。
考虑以下代码:
<?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>
输出:
- Padding :填充设置描述视图内部区域
允许视图绘制其内容的位置。
例如:更改上面代码中的填充设置:
<?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的距离之后绘制的)
- Margin : 另一方面,Margin 设置描述了可以绘制视图本身的区域。
在上面的代码中添加边距设置(并删除填充):
<?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>
输出:
如你所见,这次的视图,是在它周围留出20dp的距离后绘制的。
因此,填充和边距这两种间距技术,第一种适用于 "content of the view",后者适用于 "the view itself"。
您好,我知道使用 RelativeLayout
,我们可以使用 layout_toRightOf
、layout_toLeftOf
、layout_above
.
但是,我想知道我们如何为其添加一些间距/边距?如果我简单地设置边距,它会导致视图相对于父视图设置边距。知道如何解决这个问题吗?
不,边距将适用于视图的邻居,而不是 parents,例如,如果您将 layout_marginLeft=20dp
设置为视图 1,则视图 1 的左侧,假设视图 2,将有 20dp 的边距视图 1,根本没有 parents。
使用内边距设置视图内的边距:
android:padding="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:paddingTop="5dp"
也许向 RelativeLayout 添加 Padding 设置可能会奏效。
所以基本上有两种方法可以设置视图的间距。 边距 和边距。
考虑以下代码:
<?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>
输出:
- Padding :填充设置描述视图内部区域 允许视图绘制其内容的位置。
例如:更改上面代码中的填充设置:
<?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的距离之后绘制的)
- Margin : 另一方面,Margin 设置描述了可以绘制视图本身的区域。
在上面的代码中添加边距设置(并删除填充):
<?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>
输出:
如你所见,这次的视图,是在它周围留出20dp的距离后绘制的。
因此,填充和边距这两种间距技术,第一种适用于 "content of the view",后者适用于 "the view itself"。