第四周安卓开发学习总结(1)

写在前面

本周主要侧重学了几种基本的布局:LinearLayout,RelativeLayout,FrameLayout,TableLayout,AbsolutLayout(已过时),其中前两种侧重学习了一下。

LinearLayout

LinearLayout翻译成中文即线性布局,分为竖和横两个方向。这个布局的最大特点便是weight(权重),通过权重来分配相对应的布局,计算权重的方法即当前权重除以总权重(所有控件的权重加起来),如下示例代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button" />
</LinearLayout>

效果如图:

RelativeLayout

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">
    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="中间"
    android:layout_centerInParent="true"
    />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="右上角"
        android:layout_alignParentRight="true"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="右下角"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="左上角"
        android:layout_alignParentLeft="true"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="左下角"
        android:layout_alignParentBottom="true"
        />
</RelativeLayout>

效果图:

FrameLayout

FrameLayout即帧布局,主要为了显示单一内容,示例代码如下:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <View
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:background="#ff00"
        />
</FrameLayout>

效果图:

TableLayout

TableLayout即表格布局,如下图所示:

示例代码如下:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4"
            />

    </TableRow>
    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="5"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="6"
            />

    </TableRow>
    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="7"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="8"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="9"
            />

    </TableRow>


</TableLayout>

AbsolutLayout

AbsolutLayout已过时,即绝对布局,通过设置x和y轴来确定布局,这里不再测试了。

posted @ 2020-03-14 22:31  武神酱丶  阅读(138)  评论(0编辑  收藏  举报