android:layout_weight 属性是在LinearLayout 为各个子视图分配权重,说的通俗点就是要分配多大的视图,比如如果两个视图在全部视图里面设置为1,1,那么就是平分父视图,但是在和match_parent使用的过程中,发现有的时候并不能评分,最后在一篇大神的博客上看到了答案。

android:layout_weight的真实含义是:一旦View设置了该属性(假设有效的情况下),那么该 View的宽度等于原有宽度(android:layout_width)加上剩余空间的占比!

举个例子,下面这个布局,在TableRow里面放两个

<TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" >

            <TextView
                android:text="2017年1月19日"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/textView"
                android:layout_gravity="center"/>
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:padding="40px">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/iconfile1"
                android:id="@+id/imageView3"
                android:layout_weight="1" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/iconfile12"
                android:id="@+id/imageView2"
                android:layout_weight="1"
                android:layout_marginLeft="30px"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/iconfile13"
                android:id="@+id/imageView"
                android:layout_weight="1"
                android:layout_marginLeft="30px"/>
        </LinearLayout>
    </TableRow>

设屏幕宽度为L,这两个LinearLayout的宽度是match_parent,就是L,字啊


☟☟可点击下方广告支持一下☟☟

最后修改:2017 年 02 月 11 日
请我喝杯可乐,请随意打赏: ☞已打赏列表