【问题标题】:android setVisibility does not display if initially set to invisble如果最初设置为不可见,android setVisibility 不显示
【发布时间】:2012-03-22 08:08:51
【问题描述】:

我有一个 glsurface 占据了全屏。单击按钮时,我希望出现另一个布局(设置类型的事物)。如果我从可见的覆盖开始,我可以让它不可见,然后再次可见,没有问题。但如果我从它不可见开始,我就无法让它再次可见。代码如下:

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

    <android.opengl.GLSurfaceView
    android:id="@+id/glPlaySurface"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</android.opengl.GLSurfaceView>

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/btnRotate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:checked="true"
        android:text="R"
        android:textColor="#000" />

    <RadioButton
        android:id="@+id/btnPan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:text="P"
        android:textColor="#000" />
</RadioGroup>

<Button
    android:id="@+id/btnLights"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginLeft="15dp"
    android:layout_toRightOf="@+id/radioGroup1"
    android:text="Lights" />

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutLights"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:visibility="visible" <--- Does not work if set to invisible
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"

    android:background="#fff" >

    <Button
    android:id="@+id/btnLightsOK"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="15dp"
    android:text="OK" />

    <Button
    android:id="@+id/btnLights"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="15dp"
    android:text="OK" />

</RelativeLayout>

</RelativeLayout>


private OnClickListener mOnLightsClick = new OnClickListener() {
    public void onClick(View arg0) {
        if(mLayoutLights.getVisibility() == View.VISIBLE) {
            mLayoutLights.setVisibility(View.INVISIBLE);
        }
        else {
            mLayoutLights.setVisibility(View.VISIBLE);
        }
    }
};

【问题讨论】:

  • 问题与 glsurface 有关。如果我用普通的surfaceview替换glsurface,它可以正常工作。
  • 更多信息:如果我将 glsurface 设置为 GONE,将布局设置为 VISIBLE,然后将 glsurface 设置回 VISIBLE - 它可以工作。但是...如果我将 glsurface 设置为 INVISIBLE 而不是 GONE - 它不起作用。
  • 好的 - api 演示中的 surface_view_overlay 完全符合我的要求 - 我会看看我哪里出错了然后发布。
  • 在 UI 线程上运行设置可见性 :)

标签: android layout visibility


【解决方案1】:

有类似的错误,但这是由于我没有使用 UiThread 的愚蠢错误。

Activity act = (Activity)context;
act.runOnUiThread(new Runnable(){
@Override
public void run() {
    mLayoutLights.setVisibility(View.VISIBLE);  
} });

【讨论】:

  • 谢谢你,它解决了我的问题!我在回调中设置了可见性,不知道为什么视图没有立即更新,但现在它可以工作了。看来我需要处理 UI 线程..
【解决方案2】:

知道了。您必须设置布局中所有项目的可见性,而不仅仅是布局。所以这段代码起作用了:

if (mLayoutLights.getVisibility() == View.VISIBLE) {
    ((Button) findViewById(R.id.btnLightsOK)).setVisibility(View.GONE);
((Button) findViewById(R.id.btnLightsCnc)).setVisibility(View.GONE);
mLayoutLights.setVisibility(View.GONE);
} else {
    mLayoutLights.setVisibility(View.VISIBLE);
((Button) findViewById(R.id.btnLightsOK)).setVisibility(View.VISIBLE);
((Button) findViewById(R.id.btnLightsCnc)).setVisibility(View.VISIBLE);
}

【讨论】:

    【解决方案3】:

    就我而言,使用普通的 SurfaceView,我只是在 xml 中将视图设置为 GONE,而不是 INVISIBLE。之后我就可以正确设置 VISIBILITY 了。

    【讨论】:

    • 这真是一个非常好的答案
    【解决方案4】:

    我建议尝试三个独立的东西。

    1. 更改布局宽度和高度以包裹内容,因为这可能是匹配父项的问题。
    2. 在视图上调用 bringToFront
    3. 将 surfaceView 包装在 FrameLayout 中(这与 #2 有关,但它仍然可能有帮助)

    【讨论】:

    • 谢谢 - 但这些都没有影响。
    • 你知道渲染线程是在运行还是在表面启动?
    • 是的 - 3d 响应触摸并正确渲染(我尝试过连续渲染和脏时渲染 - 没有区别)
    • 我的意思是特别是在它开始不可见的情况下。当您启动它时,渲染器似乎没有运行。它还在运行吗?
    • 是的 - 当其他布局可见或不可见时,3d 响应并正确呈现。
    【解决方案5】:

    我遇到了同样的问题,调试日志清楚地显示可见性设置正确。

    我正在使用数据绑定。对我来说,下面的解决方案有效。

    activity.runOnUiThread(() -> binding.getRoot().post(() -> {
        binding.additionalDataHolderOne.setVisibility(View.GONE);
        binding.firstStreamHolder.setVisibility(View.VISIBLE);
    }));
    

    实际上,binding.getRoot().post(() -&gt; {}); 完成了这项工作。

    【讨论】:

      【解决方案6】:

      如果您将 CalendarView 设置为可见性“VISIBLE”,并且在 inflateView/OnCreateView 中找到它后将“findById”设置为“GONE”,您就可以随心所欲地使用它。

      日历视图 = (日历视图) view.findViewById(R.id.all_scores_calendar_view); calendarView.setVisibility(View.GONE);

      calendarView.setVisibility(View.VISIBLE);

      【讨论】:

        【解决方案7】:

        我不知道为什么不起作用(对我来说也是如此),但作为一种解决方法,您可以这样做:

         val params = mLayoutLights.layoutParams
            if (shoouldShow()) {
                params.height = WRAP_CONTENT
            } else {
                params.height = 0
            }
            mLayoutLights.layoutParams = params
        

        【讨论】:

          【解决方案8】:

          在将 2 个视图放入 LinearLayout 之前,我遇到了这样的问题。 2 个视图中的一个已消失,另一个可见。

          当我试图让消失的那个可见而可见的消失时, 它不起作用。

          我通过使用RelativeLayout而不是LinearLayout作为容器布局解决了这个问题,然后它工作正常。

          【讨论】:

            【解决方案9】:

            就我而言,我在可见性为 GONE

            的元素中调用了 .setVisiblity(View.GONE)
             if (temp.getVisibility() == View.VISIBLE) {
                        temp.setVisibility(View.GONE);
                    }
            

            做这样的事情对我有用。

            【讨论】:

              【解决方案10】:
              case R.id.title_call_button:
              if(llButtonCallNow.getVisibility() != View.VISIBLE){
              llButtonCallNow.setVisibility(View.VISIBLE);
              }
                  else{
                  llButtonCallNow.setVisibility(View.GONE);
                  Toast.makeText(getBaseContext(), ("Im here baby :)"),
                  Toast.LENGTH_SHORT).show();
              
                  }
              break;
              

              【讨论】:

                猜你喜欢
                • 2011-11-12
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-02-19
                • 1970-01-01
                • 2016-02-27
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多