我有简单的LinearLayout和2个按钮。视图看起来不错,显示中间和末端分隔线。当我以编程方式将第二个按钮的可见性设置为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>
答案 0 :(得分:1)
我认为这是LinearLayout
实施中的一个错误,因为它应该在end
按钮消失后为1_id
ID设置2_id
分隔符。但是,这不是这种情况,因此可能的解决方法是从end
中省略LinearLayout
分频器设置,并在末尾添加一个虚拟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>
答案 1 :(得分:0)
我通过复制支持库类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