2012-03-04 75 views
10

我有一个占用全屏的gsurface。点击一个按钮后,我希望显示另一个布局(设置类型的东西)。如果我首先看到叠加层,那么我可以将其隐藏起来,然后再次可见并没有问题。但是如果我从无形开始,我不能再让它看不见。代码如下:如果最初设置为不可见,则android setVisibility不会显示

<?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); 
     } 
    } 
}; 
+0

问题与glsurface有关。如果我用普通的surfaceview替换glsurface,它可以正常工作。 – user1248322 2012-03-04 19:18:00

+0

更多信息:如果我将glsurface设置为GONE,将布局设置为VISIBLE,然后将glsurface设置为VISIBLE - 它可以工作。但是...如果我将表面设置为INVISIBLE而不是GONE - 它不起作用。 – user1248322 2012-03-05 00:22:35

+0

好的 - api演示中的surface_view_overlay完全符合我的要求 - 我会看到我出错的地方,然后发布。 – user1248322 2012-03-05 01:19:32

回答

10

明白了。您必须设置布局中所有项目的可见性,而不仅仅是布局。所以这个代码工作:

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); 
} 
+1

谢谢你,我有同样的问题。 – Enrichman 2013-05-21 13:34:04

1

我会建议尝试三个独立的事情。

  1. 更改布局宽度和高度以包装内容,因为它可能是匹配父级的问题。
  2. 在视图
  3. 结束语中的FrameLayout的surfaceView调用bringToFront(这是关系到#2,但它仍可能帮助)
+0

谢谢 - 但没有一个有所作为。 – user1248322 2012-03-04 17:53:10

+0

你知道渲染线程是否正在运行,甚至是从表面开始? – BeatingToADifferentRobot 2012-03-04 18:10:09

+0

是的 - 三维响应触摸和正确渲染(我已经试过连续渲染和渲染时脏 - 没有区别) – user1248322 2012-03-04 18:35:29

-3
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; 
14

过类似的错误,但它是因为我没有使用UiThread的愚蠢的错误。

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

谢谢大家,它解决了我的问题!我在回调中设置了可见性,并不知道为什么视图没有立即更新,但现在它可以工作。似乎我需要在UI线程上工作.. – Jack 2016-12-24 11:46:44

2

在我的情况下,用普通的SurfaceView,我只是将视图设置为xml而不是INVISIBLE。之后我可以正确设置VISIBILITY。

+0

这解决了我的问题!谢谢! :) – acegs 2016-07-29 09:58:52

-2

只需将属性的宽度和高度设置为“fill_parent”而不是“match_parent”即可。不需要为所有孩子设置可见性,而只需要为更高级别的容器/元素设置可见性。

相关问题