邻居GONE / VISIBLE后,Webview(height = 0 / weight = 1)不会重新布局

时间:2016-03-12 02:34:20

标签: java android layout

我有一个布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <WebView
        android:id="@+id/map_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/skip_button"
        android:layout_width="match_parent"
        android:layout_height="@dimen/button_height"
        android:text="@string/skip"/>

</LinearLayout>

当用户在.setVisibility(View.GONE) webview中选择其坐标时,我的代码会隐藏skip_button(通过map_view)。用户还可以重置其坐标,然后按钮必须再次出现。

适用于Android 5,但即使在.setVisibility(View.VISIBLE)之后,Android 4按钮仍保持隐藏状态。我试过了:

  1. mapСontainer.getParent().requestLayout();

  2. mapСontainer.refreshDrawableState();

  3. skipButton.refreshDrawableState();

  4. 将webview包装到LinearLayout

  5. 但这一切都无济于事。

1 个答案:

答案 0 :(得分:0)

打败这个。看起来这是一个api16错误,所以我必须解决它......

if (dataOk) {
    skipButton.setVisibility(View.GONE);
    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mapСontainer.getLayoutParams();
    layoutParams.height = 0;
    layoutParams.weight = 1;
    mapСontainer.setLayoutParams(layoutParams);
    mapСontainer.getParent().requestLayout();
} else {
    int visibility = skipButton.getVisibility();
    skipButton.setVisibility(View.VISIBLE);

    if (visibility == View.GONE) {
        int height = mapСontainer.getMeasuredHeight();
        ViewGroup.LayoutParams skipParams = skipButton.getLayoutParams();
        LinearLayout.LayoutParams containerParams = (LinearLayout.LayoutParams) mapСontainer.getLayoutParams();
        containerParams.height = height - skipParams.height;
        containerParams.weight = 0;
        mapСontainer.setLayoutParams(containerParams);
    }
}