相对布局及网格布局相关示例

RelativeLayout相对布局

     关键使用:控件id的设置

    相对布局实现小五角示例

    layout布局代码

<RelativeLayout 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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn_main_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮1"/>

    <Button
        android:id="@+id/btn_main_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/btn_main_1"
        android:layout_toRightOf="@id/btn_main_1"
        android:text="按钮2" />

    <Button
        android:id="@+id/btn_main_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮3"
        android:layout_toRightOf="@id/btn_main_2"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/btn_main_2"
        android:layout_toLeftOf="@id/btn_main_2"
        android:text="按钮4" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮5"
        android:layout_below="@id/btn_main_2"
        android:layout_toRightOf="@id/btn_main_2"/>

</RelativeLayout>

效果图 

GridLayout网格布局

     关键使用:rowCount(行数)columnCount(列数)

     网格布局实现超简陋计算器

     layout布局代码

<GridLayout 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"
    android:rowCount="5"
    android:columnCount="4"
    tools:context=".MainActivity">

    <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="2" />

    <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="/"/>

    <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"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="*"/>

    <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"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnSpan="2"
        android:layout_gravity="fill_horizontal"
        android:text="0"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="."/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_rowSpan="2"
        android:layout_gravity="fill_vertical"
        android:text="+"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnSpan="3"
        android:layout_gravity="fill_horizontal"
        android:text="="/>

<Space />
</GridLayout>

效果图 

猜你喜欢

转载自blog.csdn.net/Giraffe_it/article/details/83280398