zoukankan      html  css  js  c++  java
  • android_layout_linearlayout(二)

    android的线性布局linearlayout的研究没有尽头。看了官网关于线性布局的一个例子,捣鼓一阵,发现的迷惑记录在此。

    一、先看看官网xml

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

    <LinearLayout
    android:orientation="horizontal"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="fill_parent"
    android:layout_weight
    ="1">
    <TextView
    android:text="red"
    android:gravity
    ="center_horizontal"
    android:background
    ="#aa0000"
    android:layout_width
    ="wrap_content"
    android:layout_height
    ="fill_parent"
    android:layout_weight
    ="1"/>
    <TextView
    android:text="green"
    android:gravity
    ="center_horizontal"
    android:background
    ="#00aa00"
    android:layout_width
    ="wrap_content"
    android:layout_height
    ="fill_parent"
    android:layout_weight
    ="1"/>
    <TextView
    android:text="blue"
    android:gravity
    ="center_horizontal"
    android:background
    ="#0000aa"
    android:layout_width
    ="wrap_content"
    android:layout_height
    ="fill_parent"
    android:layout_weight
    ="1"/>
    <TextView
    android:text="yellow"
    android:gravity
    ="center_horizontal"
    android:background
    ="#aaaa00"
    android:layout_width
    ="wrap_content"
    android:layout_height
    ="fill_parent"
    android:layout_weight
    ="1"/>
    </LinearLayout>

    <LinearLayout
    android:orientation="vertical"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="fill_parent"
    android:layout_weight
    ="1">
    <TextView
    android:text="row one"
    android:textSize
    ="15pt"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content"
    android:layout_weight
    ="1"/>
    <TextView
    android:text="row two"
    android:textSize
    ="15pt"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content"
    android:layout_weight
    ="1"/>
    <TextView
    android:text="row three"
    android:textSize
    ="15pt"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content"
    android:layout_weight
    ="1"/>
    <TextView
    android:text="row four"
    android:textSize
    ="15pt"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content"
    android:layout_weight
    ="1"/>
    </LinearLayout>

    </LinearLayout>

    不难理解。统大者线性布局沾满整个屏幕,里面又有两个线性布局,它们的高宽度是沾满统大者线性布局,也就是说,它们两位也是要沾满整个屏幕,你不觉得矛盾吗?看看效果图,你会更矛盾:

    我们预期看到的图应该是这样:第一个子线性布局完全沾满整个屏幕,因此,该图应该满屏幕都是红绿蓝黄,可是它居然与第二个子线性布局平起平坐,不觉得很奇怪吗?

    我仔细看了xml文件,发现两个子线性布局各加了一个属性:weight="1"。1表示两个子线性布局平分整个屏幕,这就明白了,但是仍然给人一种别扭的感觉。我将xml文件修改如下:

    二、我的xml文件

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

    <LinearLayout
    android:orientation="horizontal"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content">
    <TextView
    android:text="red"
    android:gravity
    ="center_horizontal"
    android:background
    ="#aa0000"
    android:layout_width
    ="wrap_content"
    android:layout_height
    ="wrap_content"
    android:layout_weight
    ="1"/>
    <TextView
    android:text="green"
    android:gravity
    ="center_horizontal"
    android:background
    ="#00aa00"
    android:layout_width
    ="wrap_content"
    android:layout_height
    ="wrap_content"
    android:layout_weight
    ="1"/>
    <TextView
    android:text="blue"
    android:gravity
    ="center_horizontal"
    android:background
    ="#0000aa"
    android:layout_width
    ="wrap_content"
    android:layout_height
    ="wrap_content"
    android:layout_weight
    ="1"/>
    <TextView
    android:text="yellow"
    android:gravity
    ="center_horizontal"
    android:background
    ="#aaaa00"
    android:layout_width
    ="wrap_content"
    android:layout_height
    ="wrap_content"
    android:layout_weight
    ="1"/>
    </LinearLayout>

    <LinearLayout
    android:orientation="vertical"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content">
    <TextView
    android:text="row one"
    android:textSize
    ="15pt"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content"
    android:layout_weight
    ="1"/>
    <TextView
    android:text="row two"
    android:textSize
    ="15pt"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content"
    android:layout_weight
    ="1"/>
    <TextView
    android:text="row three"
    android:textSize
    ="15pt"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content"
    android:layout_weight
    ="1"/>
    <TextView
    android:text="row four"
    android:textSize
    ="15pt"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content"
    android:layout_weight
    ="1"/>
    </LinearLayout>

    </LinearLayout>

    效果如下

    天,发生了什么!

    我将第一个子线性布局高调整为适可而止,子元素也是;第二个子线性布局也是,显示出来是这个戳样,我才明白官网为什么会那么做。

    官网要显示美观,要让两个子线性平分屏幕,怎么平分,不可能靠着子元素的字体去平分(我就是让子线性跟着字体变化而适可而止),所以靠着weight来平分。

    三、我的另一个xml文件

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

    <LinearLayout
    android:orientation="horizontal"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content"
    android:layout_weight
    ="1">
    <TextView
    android:text="red"
    android:gravity
    ="center_horizontal"
    android:background
    ="#aa0000"
    android:layout_width
    ="wrap_content"
    android:layout_height
    ="wrap_content"
    android:layout_weight
    ="1"/>
    <TextView
    android:text="green"
    android:gravity
    ="center_horizontal"
    android:background
    ="#00aa00"
    android:layout_width
    ="wrap_content"
    android:layout_height
    ="wrap_content"
    android:layout_weight
    ="1"/>
    <TextView
    android:text="blue"
    android:gravity
    ="center_horizontal"
    android:background
    ="#0000aa"
    android:layout_width
    ="wrap_content"
    android:layout_height
    ="wrap_content"
    android:layout_weight
    ="1"/>
    <TextView
    android:text="yellow"
    android:gravity
    ="center_horizontal"
    android:background
    ="#aaaa00"
    android:layout_width
    ="wrap_content"
    android:layout_height
    ="wrap_content"
    android:layout_weight
    ="1"/>
    </LinearLayout>

    <LinearLayout
    android:orientation="vertical"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content"
    android:layout_weight
    ="1">
    <TextView
    android:text="row one"
    android:textSize
    ="15pt"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content"
    android:layout_weight
    ="1"/>
    <TextView
    android:text="row two"
    android:textSize
    ="15pt"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content"
    android:layout_weight
    ="1"/>
    <TextView
    android:text="row three"
    android:textSize
    ="15pt"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content"
    android:layout_weight
    ="1"/>
    <TextView
    android:text="row four"
    android:textSize
    ="15pt"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content"
    android:layout_weight
    ="1"/>
    </LinearLayout>

    </LinearLayout>

    我就改了两个地方,分别在两个子线性布局加上属性weight=1,显示的效果:

    貌似是平分了,不过平分的不明显。

  • 相关阅读:
    [做题记录-乱做] [AGC004F] Namori
    字符串分割去除空格
    逆向实战01-嘟嘟牛在线
    mysql超8小时解决
    macbook golang的debug模式不好使
    博客暂时废弃公告
    [干货] 博客园主题
    [Contest on 2021.10.14] 我靠,真的好困!
    [Contest on 2021.10.9] 说好的 100kb 呢?
    [Contest on 2021.10.7] 已经起不了标题了...
  • 原文地址:https://www.cnblogs.com/itblog/p/2268304.html
Copyright © 2011-2022 走看看