Android中的流式布局

本文介绍了一种在Android中使用流式布局实现特定界面的方法。通过调整LinearLayout的权重和方向等属性,实现了按钮的自动排列及居中效果,最终完成了预期的布局设计。

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

 嗨,今天我来分享一下Android中的布局,布局我今天为大家分享其中一个简单的布局:流式布局          1.所谓的流式布局,就是像流水一样直接自动从左到右,自动排序

然后通过控制其属性控制控件的位置,从而达到控制其位置,大家可以试着做一下下面这个题,对于刚入门Android的童鞋来说好玩到爆(@ ̄ー ̄@)
对,不用其他布局,只用流式布局来写这个,需要动一下你的脑筋,不要小看哦,也希望有童鞋有跟我的解决方案不同的话,可以在下面回复,让本人瞄俩眼也是可以的,O(∩_∩)O哈哈~



∨下面加上我的写法





首先,将orientation属性设置为horizontal,就是将他的大Linear设置为水平

这里写图片描述

然后在定义三个小的Linear

这里写图片描述

并且将三个的Width全设置为Wrap Content ,,Height全设置为、Matc Parent
然后在每个小Linear里面先创建一个Button

<code>这里写图片描述</code>
然后设置其每个Linear的Weight为0.3

 <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="0.3">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我是左上"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="0.3">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我是中间"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="0.3">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我是右上"/>
    </LinearLayout>

附加效果图这里写图片描述

然后在设置其在所在的地方
左上的设置

 <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="0.3">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我是左上"
            android:layout_gravity="left"/>
 ![这里写图片描述](https://img-blog.youkuaiyun.com/20170605225423955?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvUFpwZW5nemhlbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)   </LinearLayout>

这样看上去没效果

再然后设置中间的

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="0.3"
        android:gravity="center">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我是中间"/>
    </LinearLayout>

附加效果图
这里写图片描述

效果显著
移到最中间了<( ̄ˇ ̄)/

然后处理右边的,移到右上角

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="0.3"
        android:gravity="right">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我是右上"/>
    </LinearLayout>

然后再附加效果图这里写图片描述
再然后处理下面两个
在右边那个小Linear属性orientation设置为垂直(Vertical),gravity=”right”
在右边那个小Linear里面嵌套一个小小Linear,并且写个Button

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="0.3"
        android:gravity="right"
        android:orientation="vertical">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我是右上"/>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.3"
            android:gravity="right">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="我是右下"/>
        </LinearLayout>
    </LinearLayout>

效果图。。。。ヾ(´∀`o)+
这里写图片描述
再设置小小Linear的button 中的layout_gravity设置为bottom


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="0.3"
        android:gravity="right"
        android:orientation="vertical">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我是右上"/>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.3"
            android:gravity="right">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="我是右下"
                android:layout_gravity="bottom"/>
        </LinearLayout>
    </LinearLayout>

=.=效果图
这里写图片描述

完美哈哈哈,然后左边的跟上面进行差不多的处理,然后写完=.=
附加最后处理完的图 今天就分享这么多,哇咔咔
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值