简体   繁体   English

线性布局中的setVisibility和layout_weight之间有什么联系

[英]What is the connection between setVisibility and layout_weight in Linear Layout

I have a LinearLayout with three children in it, two RecyclerView s and an ImageView . 我有一个带有三个孩子的LinearLayout ,两个RecyclerView和一个ImageView In the code based on some condition I enable and disable these children using setVisibility() . 在基于某些条件的代码中,我使用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:
    }
}

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. 布局文件具有一个LinearLayout其中带有第一个RecyclerView (topListView),然后是ImageView (空格),然后又是一个RecyclerView (bottomListView)。

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. 我的问题是我应该在XML中还是动态使用layout_weight属性。 and can anyone tell me what is the connection between using setVisibility() and layout_weight attribute, if there is any? 谁能告诉我使用setVisibility()layout_weight属性之间的联系setVisibility()如果有)?

Set the layout_height of your RecyclerViews to 0dp (for Vertical LinearLayout ) RecyclerViewslayout_height设置为0dp (对于Vertical LinearLayout

I have tested this working fine 我已经测试了这个工作正常

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

for hiding any view use .setVisibility(View.GONE) 隐藏任何视图使用.setVisibility(View.GONE)

for showing any view use .setVisibility(View.VISIBLE) 用于显示任何视图使用.setVisibility(View.VISIBLE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM