线性布局中setVisibility和layout_weight之间的关系是什么 [英] What is the connection between setVisibility and layout_weight in Linear Layout

查看:66
本文介绍了线性布局中setVisibility和layout_weight之间的关系是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个LinearLayout,里面有三个孩子,两个RecyclerView和一个ImageView.在基于某些条件的代码中,我使用setVisibility()启用和禁用这些子级.

I have a LinearLayout with three children in it, two RecyclerViews and an ImageView. In the code based on some condition I enable and disable these children using setVisibility().

public void onNotificationPriorityMixChanged(int mix) {
    Log.d(TAG, "onNotificationPriorityMixChanged() called with: mix = [" + mix + "]");
    switch (mix) {
        case MIX_BOTTOM_ONLY: {
            topListView.setVisibility(GONE);
            bottomListView.setVisibility(VISIBLE);
            spacer.setVisibility(GONE);
        }
        break;
        case MIX_TOP_ONLY: {
            topListView.setVisibility(VISIBLE);
            bottomListView.setVisibility(GONE);
            spacer.setVisibility(GONE);
        }
        break;
        case MIX_NONE: {
            topListView.setVisibility(GONE);
            bottomListView.setVisibility(GONE);
            spacer.setVisibility(GONE);
        }
        break;
        case MIX_BOTH: {
            topListView.setVisibility(VISIBLE);
            bottomListView.setVisibility(VISIBLE);
            spacer.setVisibility(VISIBLE);
        }
        break;
        default:
    }
}

布局文件中的LinearLayout带有第一个RecyclerView(topListView),其后是ImageView(spacer),然后是RecyclerView(bottomListView).

The layout file is has a LinearLayout with a first RecyclerView (topListView) followed by the ImageView (spacer) followed by again a RecyclerView (bottomListView) in it.

问题是,当其他两个视图被禁用时,我无法仅获得一个子视图来占据整个父视图. 我的问题是我应该在XML中还是动态使用layout_weight属性.谁能告诉我使用setVisibility()layout_weight属性之间的联系是什么?

The problem is that I cannot get only one child view to occupy whole parent when other two are disabled. My question is that should I use the layout_weight attribute in XML or dynamically. and can anyone tell me what is the connection between using setVisibility() and layout_weight attribute, if there is any?

推荐答案

RecyclerViewslayout_height设置为0dp(对于垂直LinearLayout)

Set the layout_height of your RecyclerViews to 0dp (for Vertical LinearLayout)

我已经对此进行了测试

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="2"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:layout_weight="1"
        android:visibility="visible"
        android:layout_width="match_parent"
        android:layout_height="0dp"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="2dp" />

    <android.support.v7.widget.RecyclerView
        android:layout_weight="1"
        android:visibility="visible"
        android:layout_width="match_parent"
        android:layout_height="0dp"/>

</LinearLayout>

用于隐藏的任何视图均使用.setVisibility(View.GONE)

for hiding any view use .setVisibility(View.GONE)

用于显示的任何视图使用.setVisibility(View.VISIBLE)

这篇关于线性布局中setVisibility和layout_weight之间的关系是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆