android:layout_weight =“33”怎么回事 - >可见的可见 - >效果如何?

时间:2013-01-08 23:27:16

标签: android android-layout

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

2 个答案:

答案 0 :(得分:0)

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

((LinearLayout.LayoutParams) btnFav.getLayoutParams()).weight = 3f;
btnFav.refreshDrawableState();

答案 1 :(得分:-1)

确保将高度设置为0dp,然后将所有视图的权重设置为1

<?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>

当视图设置为已消失时,其余视图将填满其屏幕比例。即如果您将第三个视图设置为已消失,则前两个视图将占用布局中每个可用空间的50%

此外,如果你想让其余的视图每个只占用1/3的空间(即使用2/3并留下1/3为空),将隐藏的视图设置为不可见的