Android布局2
表格布局
表格布局就是让控件以表格形式来排列组件的,只要将组件或信息放在单元格中,控件就可以整齐的排列
在TableLayout中,行数由TableRow对象控制的,即布局中有多少TableRow对象,就有多少行
部分代码如下:
<androidx.constraintlayout.widget.ConstraintLayout 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=“match_parent”>
<TableLayout
android:layout_width="224dp"
android:layout_height="727dp"
android:layout_marginEnd="4dp"
android:layout_marginRight="4dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<TableRow
android:layout_width="match_parent"
android:layout_height="110dp" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TableLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="283dp"
tools:layout_editor_absoluteY="102dp" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="283dp"
tools:layout_editor_absoluteY="316dp" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="256dp"
tools:layout_editor_absoluteY="472dp"
tools:srcCompat="@tools:sample/avatars" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="34dp"
tools:layout_editor_absoluteY="259dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
网格布局GridLayout
可以自己设置布局中的排列方式
可以自己定义网格布局有多少行和列
可以直接设置组件位于某行某列
可以设置组件横跨几行或者几列
部分代码如下
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="4"
android:columnCount="4"
tools:layout_editor_absoluteX="190dp"
tools:layout_editor_absoluteY="288dp">
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
FrameLayout帧布局
Android :foregeround:设置帧布局容器的前景图像
Android :foregeroundgravity:设置前景图像的显示位置
部分代码如下
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="85dp"
tools:layout_editor_absoluteY="49dp">
<TextView
android:id="@+id/你好"
android:layout_width="500dp"
android:layout_height="300dp"
android:background="@color/purple_500"
android:text="你好" />
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>