java 相对布局在 view.setVisibility(View.GONE) 和 view.setVisibility(View.VISIBLE) 后刷新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17398975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:53:15  来源:igfitidea点击:

RelativeLayout refresh after view.setVisibility(View.GONE) and view.setVisibility(View.VISIBLE)

javaandroidlayoutvisibilityrelativelayout

提问by Marek

Please look at the following image to understand the problem:

请查看下图以了解问题所在:

enter image description here

在此处输入图片说明

As you can see, there is a RelativeLayoutwhich holds a custom Viewand a LinearLayout.
Between them, there is also another View, which Visibilityis set to GONE. Now, when I press the Button, I would like to change the visibility of that GONEView to VISIBLE, and rearrange the RelativeLayout, so that the third Viewgets between custom Viewand LinearLayout. When I press another button, I would like to make third view GONE again. How can I achieve that?

如您所见,有一个RelativeLayout包含 customViewLinearLayout.
在它们之间,还有另一个ViewVisibility设置为GONE。现在,当我按下 时Button,我想将该GONE视图的可见性更改为VISIBLE,并重新排列RelativeLayout,以便第三个View介于自定义ViewLinearLayout. 当我按下另一个按钮时,我想让第三个视图再次消失。我怎样才能做到这一点?

Here is my XML:

这是我的 XML:

<view
    android:id="@+id/CanvasView"
    android:layout_width="200dp"
    android:layout_height="40dp"
    android:layout_above="@+id/HorizontalScrollView"
    android:layout_centerHorizontal="true"
    android:layout_margin="0dp"
    android:layout_marginBottom="0dp"
    android:layout_marginTop="0dp"
    class="com.example.CanvasView"
    android:orientation="vertical" />

<HorizontalScrollView
    android:id="@+id/HorizontalScrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/linearLayout2"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:visibility="gone" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:orientation="horizontal" >

    //SOME BUTTONS HERE
    </LinearLayout>

</HorizontalScrollView>

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginTop="5dp" >

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:layout_weight="0.4"
        android:maxWidth="200dp"
        android:text="Clear"
        android:textColor="@android:color/white"
        android:textSize="20sp" />

    <Button
        android:id="@+id/settingsButton"
        android:layout_width="44dp"
        android:layout_height="44dp"
        android:layout_gravity="center_vertical|center_horizontal"
        android:background="@drawable/settings_button_selector" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:layout_weight="0.4"
        android:enabled="false"
        android:maxWidth="200dp"
        android:text="Recognize"
        android:textColor="@android:color/white"
        android:textSize="20sp" />

</LinearLayout>

If I only setVisibility of the third view in OnClick() method, it will appear above LinearLayout and it will overlay the custom View (I tried it before). In other words, it will not move RelativeLayout up.

如果我只在 OnClick() 方法中 setVisibility 的第三个视图,它会出现在 LinearLayout 之上,它将覆盖自定义视图(我之前尝试过)。换句话说,它不会上移RelativeLayout。

回答by Alex Gittemeier

findViewById(R.id.button1).setOnClickListener(new OnClickListener(){
     public void onClick(View v) {
         findViewById(R.id.hiddenview).setVisibility(View.GONE);
         findViewById(R.id.relativelayout).invalidate();
     }
});
findViewById(R.id.button2).setOnClickListener(new OnClickListener(){
     public void onClick(View v) {
         findViewById(R.id.hiddenview).setVisibility(View.VISIBLE);
         findViewById(R.id.relativelayout).invalidate();
     }
});

回答by 7bluephoenix

In your activity, onClick()get the instance of that GONEview to View.VISIBLEso that it reappears between the two view.

在您的活动中,onClick()获取该GONE视图的实例,View.VISIBLE以便它重新出现在两个视图之间。

 viewName.setVisibility(View.VISIBLE);

For this that in between VIEWshould already be present in the xml, just that it is GONEby default!

为此,两者之间VIEW应该已经存在于 xml 中,只是GONE默认情况下!