安卓开发从0到1(基础知识8-设置视图的间距)
设置视图间距有两种方式:
-
采用layout_margin属性,它指定了当前视图与周围平级视图之间的距离。包括layout_margin,layout_marginLeft,layout_marginTop,layout_marginRight,layout_marginBottom
-
采用padding属性,它指定了当前视图与内部下级视图之间的距离。包括padding,paddingLeft,paddingTop,paddingRight,paddingBottom
第一步:创建SetMarginActivity.java
第二步:在对应的xml文件中
<?xml version="1.0" encoding="utf-8"?> <!--最外层的布局颜色为蓝色--> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="300dp" android:orientation="vertical" android:background="#00AAFF" > <!--中间层的布局为黄色 --> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="20dp" android:background="#FFFF99" android:padding="60dp" > <View android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FF0000"></View> </LinearLayout> </LinearLayout>
打开Design:
其中蓝色的宽度为20dp,黄色的宽度为60dp。