线性布局LinearLayout消消乐界面

本文详细介绍了Android中layout_margin和layout_padding的区别,以及如何使用match_parent和wrap_content实现灵活布局。通过实例演示了如何创建四行按钮布局,利用weight属性实现等宽分布,并在values文件中定义统一的按钮样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

知识点:

android:layout_margin:设置当前布局与屏幕边界,周围布局或控件的距离,有android:layout_marginTop上边界、android:layout_marginBottom下边界、android:layout_marginLeft左边界、android:layout_marginRight有边界

android:layout_padding:设置当前布局与该布局控件的距离

android:layout_width:match_parent:表示该布局宽度跟父容器相同

android:layout_height:wrap_content:表示该布局高度恰好能包裹他的内容

首先把需要的图片复制到drawable目录下:

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAZGVuZ2ZlbmdsaW5nOTk5,size_11,color_FFFFFF,t_70,g_se,x_16

 

编写布局文件activity_main.xml,代码如下:

首先把外围打布局更改为线性布局LinearLayout,添加背景:android:background="@drawable/two",在其中再添加4行线性布局LinearLayout,在每个线性布局里面添加4个按钮

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:paddingLeft="15dp"
    tools:context=".MainActivity"
    android:background="@drawable/animal_bg"
    >


    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="250dp">
    <Button
        style="@style/btnStyle"
        android:background="@drawable/two">
    </Button>
    <Button
        style="@style/btnStyle"
        android:background="@drawable/one">
    </Button>
    <Button
        style="@style/btnStyle"
        android:background="@drawable/three">
    </Button>
    <Button
        style="@style/btnStyle"
        android:background="@drawable/four">
    </Button>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <Button
            style="@style/btnStyle"
            android:background="@drawable/two">
        </Button>
        <Button
            style="@style/btnStyle"
            android:background="@drawable/one">
        </Button>
        <Button
            style="@style/btnStyle"
            android:background="@drawable/four">
        </Button>
        <Button
            style="@style/btnStyle"
            android:background="@drawable/three">
        </Button>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <Button
            style="@style/btnStyle"
            android:background="@drawable/one">
        </Button>
        <Button
            style="@style/btnStyle"
            android:background="@drawable/two">
        </Button>
        <Button
            style="@style/btnStyle"
            android:background="@drawable/four">
        </Button>
        <Button
            style="@style/btnStyle"
            android:background="@drawable/three">
        </Button>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <Button
            style="@style/btnStyle"
            android:background="@drawable/two">
        </Button>
        <Button
            style="@style/btnStyle"
            android:background="@drawable/three">
        </Button>
        <Button
            style="@style/btnStyle"
            android:background="@drawable/four">
        </Button>
        <Button
            style="@style/btnStyle"
            android:background="@drawable/one">
        </Button>
    </LinearLayout>
</LinearLayout>

 

按钮的样式都是相同的所以在values文件夹下,style.xml里添加样式:在用的时候调用就可以了,来简化代码。

把宽度设为0dp,权重设为1,就是设置以上一个线性布局里面的4个按钮控件平分线性布局,每个所占比例相等

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAZGVuZ2ZlbmdsaW5nOTk5,size_13,color_FFFFFF,t_70,g_se,x_16

 style.xml详细代码如下:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="btnStyle">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_weight">1</item>
        <item name="android:layout_marginRight">15dp</item>
        <item name="android:layout_marginBottom">15dp</item>
        <item name="android:layout_height">match_parent</item>
    </style>

</resources>

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAZGVuZ2ZlbmdsaW5nOTk5,size_9,color_FFFFFF,t_70,g_se,x_16

打开逍遥模拟器,运行项目结果: 

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAZGVuZ2ZlbmdsaW5nOTk5,size_12,color_FFFFFF,t_70,g_se,x_16

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喵俺第一专栏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值