视图指 View 类,所有的控件类(如 TextView、Button等)都继承自 View 类,可以说程序内可见的所有控件都是视图
设置视图宽高
在 XML 文件中,通过属性 android:layout_width 设置视图宽度,通过属性 android:layout_height 设置视图高度,主要的取值有:
- match_parent:匹配父组件,就是父组件多大,子组件就多大
- wrap_content:包裹内容,与内容自适应
- 具体尺寸,以 dp 为单位
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <TextView android:id="@+id/text3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello world (wrap_content)" android:textSize="15sp"/> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="hello world (match_parent)" android:textSize="15sp" /> <TextView android:layout_width="350dp" android:layout_height="100dp" android:text="hello world (firm size)" android:textSize="15sp"/>
|
在 Java 代码中,要设置视图的宽高,需要先确保它在 XML 中的宽高属性被设置为 wrap_content ,然后获取控件对象,再依次执行以下步骤(参数单位为 px ,注意单位转换):
- 调用 getLayoutParams 方法获取布局参数
- 修改 width 和 height 参数,分别表示宽度和高度
- 调用 setLayoutParams 方法修改布局参数
也可以使用 setWidth 和 setHeight 方法来设置宽高属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv = findViewById(R.id.set_by_code); ViewGroup.LayoutParams params = tv.getLayoutParams(); params.width = 500; params.height = 300; tv.setLayoutParams(params); tv.setWidth(500); tv.setHeight(300); }
|
设置视图间距
在 XML 文件中,有两种属性定义视图间距:
- layout_margin 属性:外边距,指定当前视图与周围平级视图之间的距离(包括margin、marginTop、marginBottom、marginLeft、marginRight,即本体和上下左右)
- padding 属性:内边距,指定当前视图与内部下级视图之间的距离(也包括本体和上下左右)
**注意:**设置本体的值,就是一起设置上下左右的值(如margin = 10dp,则上下左右的值都是 10dp )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/teal_200" android:orientation="vertical" android:gravity="center" android:padding="10dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="300dp" android:background="#3388FF"/>
<LinearLayout android:layout_width="match_parent" android:layout_height="300dp" android:background="@color/purple_200" android:layout_margin="20dp" android:padding="10dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#AABBCC"/> </LinearLayout>
</LinearLayout>
|
设置视图对齐方式
在 XML 文件中,有两个属性设置视图对齐方式:
- layout_gravity 属性:当前视图相对于上级视图的对齐方式
- gravity 属性:下级视图相对于当前视图的对齐方式
这两个属性的取值有:left、top、right、bottom等,这些取值可以使用 ”|“ 连接,比如 ” left|top “ 指靠左上对齐
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="300dp" android:background="@color/teal_200" android:orientation="horizontal">
<LinearLayout android:layout_width="0dp" android:layout_height="200dp" android:layout_margin="10dp" android:layout_weight="1" android:background="#3388FF" android:padding="10dp" android:gravity="left" android:layout_gravity="bottom">
<LinearLayout android:layout_width="50dp" android:layout_height="50dp" android:background="#FFEEAA"/>
</LinearLayout>
<LinearLayout android:layout_width="0dp" android:layout_height="200dp" android:layout_margin="10dp" android:layout_weight="1" android:background="#3388FF" android:padding="10dp" android:gravity="right" android:layout_gravity="top"> <LinearLayout android:layout_width="50dp" android:layout_height="50dp" android:background="#AABBCC"/>
</LinearLayout>
</LinearLayout>
|