9

android 布局使用 layout_weight。我的目标是所有组件的 1/3,但有时页脚实际上设置为消失,然后可见。当设置从消失到可见时,重量计算如何工作?我没有看到重量为 1/3 的线性布局中的内容?

4

2 回答 2

0

在更改视图的权重后,似乎有必要在视图上调用 refreshDrawableState() 才能呈现更改:

((LinearLayout.LayoutParams) btnFav.getLayoutParams()).weight = 3f;
btnFav.refreshDrawableState();
于 2015-03-11T20:37:30.743 回答
-1

make sure you set height to 0dp and then set the weight on all of the views to 1

ie

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="Hello World, MyActivity"
        android:background="#f00"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="Hello World, MyActivity"
        android:background="#00f"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="Hello World, MyActivity"
        android:background="#0f0"
        />
</LinearLayout>

when a view is set to gone the remaining views will fill up their ratio of the screen. Ie if you set the third view to gone the first 2 views will take up 50% each of the available space in the layout

Furthermore, if you want the remaining views to only take up 1/3 of space each (ie 2/3 used and leave 1/3 empty) set your hidden view to invisible not gone

于 2013-01-08T23:40:04.107 回答