Android setVisibility не отображается, если изначально установлено значение invisble
У меня есть glururface, занимающий весь экран. Одним нажатием кнопки я хочу отобразить другой макет (тип настроек). Если я начну с видимого наложения, я могу сделать его невидимым, а затем снова видимым без проблем. Но если я начну с него невидимым, я не смогу снова увидеть его снова. Код следует:
<?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
Получил это. Вы должны установить видимость всех элементов в макете, а не только макет. Итак, этот код работал:
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);
}
Ответ 2
Имел подобную ошибку, но это было связано с моей глупой ошибкой в том, что вы не используете UiThread.
Activity act = (Activity)context;
act.runOnUiThread(new Runnable(){
@Override
public void run() {
mLayoutLights.setVisibility(View.VISIBLE);
} });
Ответ 3
Я предлагаю попробовать три независимые вещи.
- Изменение ширины и высоты макета для обертывания содержимого, поскольку это может быть проблемой при сопоставлении родительского элемента.
- Вызов showToFront в представлении
- Обтекание поверхностиView в FrameLayout (это связано С# 2, но оно все равно может помочь)
Ответ 4
В моем случае, с простым SurfaceView, я просто установил View to GONE в xml, а не INVISIBLE. Затем я смогу правильно установить VISIBILITY.
Ответ 5
Просто установите ширину и высоту свойств как "fill_parent" вместо "match_parent". Нет необходимости устанавливать видимость для всех детей, но только для контейнера/элемента более высокого уровня.
Ответ 6
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;