Android常见界面布局
布局是指对界面结构的全面规划与安排,通过api中提供的各种布局能够快速的完成对界面的设计
1.常用布局
-
线性布局(LinearLayout)
<?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:layout_margin="20dp" android:orientation="horizontal" android:padding="0dp"> <!--vertical:垂直的 horizontal:水平的--> <!--layout_weight:权重--> <!-- android:layout_margin="20dp" android:padding="0dp" --> <!--调节位置空间及比例,最后分配android:layout_weight="1"--> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:background="#ff0000" android:text="冲冲冲冲冲冲冲冲冲冲冲冲冲冲冲" android:layout_weight="1" android:textSize="28sp" android:layout_gravity="bottom"/> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:background="#00ff00" android:text="冲冲冲" android:layout_weight="1" android:textSize="28sp" android:layout_gravity="center"/> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:background="#0000ff" android:text="冲冲冲" android:layout_weight="1" android:textSize="28sp" /> </LinearLayout>
-
相对布局(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"> <!--android:layout_centerInParent 在父容器居中 android:layout_centerVertical="true" 水平居中 1. 在参照物的某边 android:layout_toLeftOf 在组件左边 2. 与参照物某边对齐 android:layout_alignLeft --> <TextView android:id="@+id/center" android:layout_width="100dp" android:layout_height="100dp" android:textSize="30sp" android:text="屏幕正中" android:textColor="#ff0000" android:layout_centerInParent="true" /> <TextView android:layout_width="100dp" android:layout_height="100dp" android:textSize="30sp" android:text="中偏左上" android:textColor="#00ff00" android:layout_toLeftOf="@id/center" android:layout_above="@+id/center"/> <TextView android:layout_width="100dp" android:layout_height="100dp" android:textSize="30sp" android:text="中偏右上" android:textColor="#00ff00" android:layout_toRightOf="@id/center" android:layout_above="@+id/center"/> <TextView android:layout_width="100dp" android:layout_height="100dp" android:textSize="30sp" android:text="中偏左下" android:textColor="#00ff00" android:layout_toLeftOf="@id/center" android:layout_below="@+id/center"/> <TextView android:layout_width="100dp" android:layout_height="100dp" android:textSize="30sp" android:text="中偏右下" android:textColor="#00ff00" android:layout_toRightOf="@id/center" android:layout_below="@+id/center"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="边线对" android:textSize="30sp" android:background="#0000ff" android:layout_alignLeft="@id/center"/> </RelativeLayout>
-
帧布局(FrameLayout)
-
表格布局(TableLayout)
-
网格布局(GridLayout)
-
约束布局(ConstraintLayout)
2.添加布局
-
利用xml文件设计
-
使用java代码添加
package com.example.testmodule; import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.ViewGroup; import android.widget.LinearLayout; public class CodeForLayout extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R. layout . activity_ main); //1.设置布局为线性布局 LinearLayout ll = new LinearLayout(this); //2.设置宽高 ll.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); //3.背景设为红色 ll.setBackgroundColor(Color.RED); //4.指定此Activity的内容视图为该线性布局 setContentView(ll); } }
3.布局重要属性
android:layout_width 宽度
android:layout_height 高度
android:layout_margin 外边距
android:layout_padding 内边距
============================
线性布局重要属性
android:orientation 方向
android:layout_weight 权重
android:layout_gravity
============================
相对布局重要属性
android:layout_centerInParent 在父容器居中
android:layout_centerVertical="true" 水平居中
1. **在参照物的某边**
**android:layout_toLeftOf 在组件左边**
2. **与参照物某边对齐**
**android:layout_alignLeft**