加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

layout_weight 小白的理解之我的地盘听我的!(二)

发布时间:2020-12-16 09:36:55 所属栏目:百科 来源:网络整理
导读:上篇文章比较详细的写了写layout_weght的原理,怎么让他听从咱们的指挥,说让他占几分地,他就得占几分地(好像咱是地主一样哈,嘿嘿)。 解决的方法就是0px,官方不是也推荐这么写吗,哈哈,不过咱们还得封装一下,看下自己的style(style_layout.xml),写

上篇文章比较详细的写了写layout_weght的原理,怎么让他听从咱们的指挥,说让他占几分地,他就得占几分地(好像咱是地主一样哈,嘿嘿)。

解决的方法就是0px,官方不是也推荐这么写吗,哈哈,不过咱们还得封装一下,看下自己的style(style_layout.xml),写在values目录下就ok啦

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!-- 全屏幕拉伸 -->
    <style name="layout_full">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>
    </style>

    <!-- 固定自身大小 -->
    <style name="layout_wrap">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
    </style>

    <!-- 横向分布 -->
    <style name="layout_horizontal" parent="layout_full">
        <item name="android:layout_width">0px</item>
    </style>

    <!-- 纵向分布 -->
    <style name="layout_vertical" parent="layout_full">
        <item name="android:layout_height">0px</item>
    </style>

</resources>
使用的方法就是如果在横向的布局下使用layout_horizontal,纵向的使用layout_vertical,看例子:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        style="@style/layout_vertical"
        android:layout_weight="3"
        android:orientation="horizontal" >

        <TextView
            style="@style/layout_horizontal"
            android:layout_weight="1"
            android:background="#123456"
            android:gravity="center"
            android:text="weight = 1"
            android:textColor="#ffffff" />

        <TextView
            style="@style/layout_horizontal"
            android:layout_weight="2"
            android:background="#234567"
            android:gravity="center"
            android:text="weight = 2"
            android:textColor="#ffffff" />

        <TextView
            style="@style/layout_horizontal"
            android:layout_weight="3"
            android:background="#345678"
            android:gravity="center"
            android:text="weight = 3"
            android:textColor="#ffffff" />
    </LinearLayout>

    <TextView
        style="@style/layout_vertical"
        android:layout_weight="2"
        android:background="#456789"
        android:gravity="center"
        android:text="weight = 2"
        android:textColor="#ffffff" />

    <TextView
        style="@style/layout_vertical"
        android:layout_weight="1"
        android:background="#567890"
        android:gravity="center"
        android:text="weight = 1"
        android:textColor="#ffffff" />

</LinearLayout>
效果如下:


是不是我让他占几分地就得占几分地啊,并且这个做起适配来也比较轻松,不过只能适配那些比较粗略的布局,要是要求比较高的话这个会不怎么实用。

好啦,代码0分下载:

点击打开链接

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读