簡體   English   中英

在LinearLayout中將最后一個視圖的可見性設置為GONE時,缺少底部分隔線

[英]Bottom divider is missing when visibility of the last view is set to GONE in LinearLayout

我有2個按鈕的簡單LinearLayout。 視圖看起來不錯,顯示了中間和末端分隔線。 當我以編程方式將第二個按鈕的可見性設置為View.GONE時,第一個按鈕下面的分隔線丟失。 怎么改變呢?

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="?android:attr/listDivider"
    android:orientation="vertical"
    android:showDividers="middle|end"
    android:animateLayoutChanges="true">

    <Button
        android:id="@+id/1_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="1"
        android:layout_marginLeft="@dimen/big_margin"
        android:layout_marginTop="@dimen/normal_margin"
        android:layout_marginRight="@dimen/big_margin"
        android:layout_marginBottom="@dimen/normal_margin"/>

    <Button
        android:id="@+id/2_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="2"
        android:layout_marginLeft="@dimen/big_margin"
        android:layout_marginTop="@dimen/normal_margin"
        android:layout_marginRight="@dimen/big_margin"
        android:layout_marginBottom="@dimen/normal_margin"/>
</LinearLayout>

我認為這是LinearLayout實現中的錯誤,因為它應該在2_id按鈕消失后為具有1_id id的按鈕設置end分隔符。 但是,這里不是這種情況,因此可能的解決方法是從LinearLayout忽略end分隔器設置,並在末尾添加一個虛擬View ,以便它的中間分隔器將模擬最后一個分隔器。

看這個例子:

<!-- No 'end' divider in 'android:showDividers' attribute -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:showDividers="middle"
    android:divider="?android:attr/listDivider"
    android:orientation="vertical">

    <TextView
        android:text="Line 1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:text="Line 2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:text="Last line"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <!-- Dummy view to simulate last divider -->
    <View
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

我通過制作支持庫類LinearLayoutCompat的副本並修改以下兩個方法來解決此問題:

void drawDividersVertical(Canvas canvas) {
    final int count = getVirtualChildCount();
    View lastVisibleChild = null;

    for (int i = 0; i < count; i++) {
        final View child = getVirtualChildAt(i);
        if (child != null && child.getVisibility() != GONE) {
            lastVisibleChild = child;
            if (hasDividerBeforeChildAt(i)) {
                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
                final int top = child.getTop() - lp.topMargin - mDividerHeight;
                drawHorizontalDivider(canvas, top);
            }
        }
    }

    if (hasDividerBeforeChildAt(count)) {
        final View child = lastVisibleChild;
        int bottom = 0;
        if (child == null) {
            bottom = getHeight() - getPaddingBottom() - mDividerHeight;
        } else {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            bottom = child.getBottom() + lp.bottomMargin;
        }
        drawHorizontalDivider(canvas, bottom);
    }
}

protected boolean hasDividerBeforeChildAt(int childIndex) {
    if (childIndex == 0) {
        return (mShowDividers & SHOW_DIVIDER_BEGINNING) != 0;
    } else if (childIndex == getChildCount()) {
        return (mShowDividers & SHOW_DIVIDER_END) != 0;
    } else if ((mShowDividers & SHOW_DIVIDER_MIDDLE) != 0) {
        boolean hasVisibleViewBefore = false;
        for (int i = childIndex - 1; i >= 0; i--) {
            if (getChildAt(i).getVisibility() != GONE) {
                hasVisibleViewBefore = true;
                break;
            }
        }
        return hasVisibleViewBefore || (mShowDividers & SHOW_DIVIDER_BEGINNING) != 0;
    }
    return false;
}

https://code.google.com/p/android/issues/detail?id=200396&thanks=200396&ts=1454685515

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM