android setVisibility does not display if initially set to invisble

不羁的心 提交于 2019-12-30 01:42:05

问题


I have a glsurface occupying the full screen. At the click of a button I want another layout to appear (settings type of thing). If I start with the overlay being visible, I can make it invisible and then visible again with no problem. But if I start with it invisible, I cannot make it ever visible again. Code follows:

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

回答1:


Had similar error but it was due to my silly mistake of not using the UiThread.

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



回答2:


Got it. You have to set the visibility of all the items in the layout, not just the layout. So this code worked:

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:


In my case, with a plain SurfaceView, I just set the View to GONE in xml, not INVISIBLE. Then I can set VISIBILITY correctly after that.




回答4:


I would suggest trying three independent things.

  1. Changing layout width and height to wrap content as it could be an issue with matching the parent.
  2. Calling bringToFront on the view
  3. Wrapping the surfaceView in a FrameLayout (this is related to #2, but it still might help)



回答5:


if you set the CalendarView to visibility "VISIBLE" and in the inflateView/OnCreateView after you find it with "findById" just set to "GONE" and you can do what ever you want with it.

calendarView = (CalendarView) view.findViewById(R.id.all_scores_calendar_view); calendarView.setVisibility(View.GONE);

calendarView.setVisibility(View.VISIBLE);




回答6:


I have faced the same problem and debug log was clearly showing that the visibility is correctly set.

I was using databinding. For me the solution below worked.

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

Actually, binding.getRoot().post(() -> {}); did the job.




回答7:


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;


来源:https://stackoverflow.com/questions/9556730/android-setvisibility-does-not-display-if-initially-set-to-invisble

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!