setVisibility(View.VISIBLE)并不总是有效。想法? [英] setVisibility(View.VISIBLE) doesn't always work. Ideas?

查看:1000
本文介绍了setVisibility(View.VISIBLE)并不总是有效。想法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图表现出一对隐藏的按钮(使用 setVisibility(View.VISIBLE),在 RelativeLayout的),但它并不总是工作。该按钮会显示在的Galaxy Tab 10.1,而不是在一个更小的平板电脑OK(不知道哪种模式),也没有在Android 4.0模拟器。

我随机发现,一定 TextView的牛逼,以下code使按钮变得可见:

  t.setText(t.getText());
...
button.setVisibility(View.VISIBLE);
 

T 位于同一 RelativeLayout的,但没有相关的按钮(其位置是独立的,非-overlapping)。

修改:在某些情况下,Android开发人员要跟踪下来......

我就能够减少code以下布局显示出这个问题在Android 4.0.3模拟器,但不是的Galaxy Tab。我发现,我需要一个 SurfaceView 或不会发生的问题(例如,将其更改为的TextView 和问题消失)。

 < XML版本=1.0编码=UTF-8&GT?;
<! - 布局/的test.xml  - >
< RelativeLayout的
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / RelativeLayout的
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>

    < SurfaceView
        机器人:ID =@ + ID / mapCtrl
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:layout_above =@ + ID / bottomPanel
        机器人:文本=占位符
        机器人:layout_marginTop =18dip/>
    <的TextView
        机器人:ID =@ + ID /标题
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentTop =真
        机器人:layout_centerHorizo​​ntal =真
        机器人:文本=@字符串/ map_mode_title/>

    &所述;! -  ============================================= ====  - >
    <  - 下吧!当前道路名称和当前速度 - >
    <的LinearLayout
        机器人:ID =@ + ID / bottomPanel
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentBottom =真
        机器人:后台=#F228
        机器人:方向=横向
        机器人:文字颜色=#FFFF>
        <按钮
            机器人:ID =@ + ID / btnNavMode
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =FILL_PARENT
            机器人:layout_marginRight =3dip
            机器人:文字颜色=#FFF
            机器人:文本=切换到\ nNav模式/>
        < RelativeLayout的
            机器人:ID =@ + ID / currentStreetPanel
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =1
            机器人:的onClick =的onClick
            机器人:可点击=真
            机器人:方向=垂直>
            <的TextView
                机器人:ID =@ + ID / currentStreetHdg
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_alignParentLeft =真
                机器人:layout_alignParentTop =真
                机器人:文本=当前一条街
                机器人:文字颜色=#FFF
                机器人:TEXTSIZE =10dip/>
            <的TextView
                机器人:ID =@ + ID / currentStreet
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_alignParentLeft =真
                机器人:layout_below =@ + ID / currentStreetHdg
                机器人:layout_marginTop = -  8DIP
                机器人:单线=真
                机器人:文本=当前一条街
                机器人:文字颜色=#FFF
                机器人:TEXTSIZE =30dip/>
        < / RelativeLayout的>
        < RelativeLayout的
            机器人:ID =@ + ID / RelativeLayout2
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =match_parent
            机器人:后台=#ff606060
            机器人:方向=垂直>
            <的TextView
                机器人:ID =@ + ID / yourSpeed​​Hdg
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_alignParentLeft =真
                机器人:layout_alignParentTop =真
                机器人:layout_marginLeft =3dip
                机器人:文本=你的速度
                机器人:文字颜色=#FFF
                机器人:TEXTSIZE =10dip/>
            <的TextView
                机器人:ID =@ + ID /速度
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_alignParentLeft =真
                机器人:layout_below =@ + ID / yourSpeed​​Hdg
                机器人:layout_marginLeft =3dip
                机器人:layout_marginTop = -  8DIP
                机器人:文本=0
                机器人:文字颜色=#FFF
                机器人:TEXTSIZE =30dip/>
            <的TextView
                机器人:ID =@ + ID / speedUnit
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_alignBaseline =@ + ID /速度
                机器人:layout_marginLeft =5dip
                机器人:layout_toRightOf =@ + ID /速度
                机器人:文本=KPH
                机器人:文字颜色=#FFF
                机器人:TEXTSIZE =18dip/>
        < / RelativeLayout的>
    < / LinearLayout中>

    <! -  =====  - >
    <! - 在地图按钮 - >
    <按钮
        机器人:ID =@ + ID / btnClearRoute
        机器人:后台=#F00
        机器人:文字颜色=#FFF
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=清除\ nroute/>
    < ZoomControls
        机器人:ID =@ + ID / zoomControls
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignBottom =@ + ID / mapCtrl
        机器人:layout_centerHorizo​​ntal =真
        机器人:layout_marginBottom = -  25dip
        机器人:方向=横向/>
    <按钮
        机器人:ID =@ + ID / btnFindRoute
        机器人:layout_width =100dip
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignBottom =@ + ID / mapCtrl
        机器人:layout_alignParentRight =真
        机器人:layout_marginRight =2DIP
        机器人:layout_marginBottom =65dip
        机器人:文本=路线选定的位置
        机器人:TEXTSIZE =17dip/>
    <按钮
        机器人:ID =@ + ID / btnUnselect
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignBottom =@ + ID / btnFindRoute
        机器人:layout_alignTop =@ + ID / btnFindRoute
        机器人:layout_alignParentLeft =真
        机器人:layout_marginLeft =2DIP
        机器人:文本=取消选择/>
    <的LinearLayout
        机器人:ID =@ + ID / showMePanel
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_above =@ + ID / btnFindRoute
        机器人:layout_alignRight =@ + ID / btnFindRoute
        机器人:layout_alignLeft =@ + ID / btnFindRoute
        机器人:填充=4dip
        机器人:后台=#BBBB
        机器人:重力=中心
        机器人:方向=垂直>
        <的TextView
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=给我......
            机器人:文字颜色=#FFF/>
        <按钮
            机器人:ID =@ + ID / btnShowVehicle
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=我的车/>
        <按钮
            机器人:ID =@ + ID / btnShowRoute
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=路由/>
        <按钮
            机器人:ID =@ + ID / btnShowDestination
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=目的地/>
        <按钮
            机器人:ID =@ + ID / btnShowMap
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=地图/>
    < / LinearLayout中>
< / RelativeLayout的>
 

Activity类简单地切换,当任一按钮被点击这两个按钮的可见性。此外,在某些设备上它的工作原理,别人没有。

 包mentor.simplegps;

进口android.app *。
进口android.os.Bundle;
进口android.view *。
进口android.widget *。

公共类TestActivity扩展活动实现View.OnClickListener
{
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        的setContentView(R.layout.test);
        样板();
        setVisibilities();
    }

    按钮_btnShowMap,_btnShowVehicle,_btnShowRoute,_btnShowDestination;
    按钮_btnUnselect,_btnFindRoute,_btnNavMode;
    TextView的_title;

    无效样板()
    {
        _btnUnselect = attachBtn(R.id.btnUnselect);
        _btnShowMap = attachBtn(R.id.btnShowMap);
        _btnShowVehicle = attachBtn(R.id.btnShowVehicle);
        _btnShowRoute = attachBtn(R.id.btnShowRoute);
        _btnShowDestination = attachBtn(R.id.btnShowDestination);
        _btnFindRoute = attachBtn(R.id.btnFindRoute);
        _btnNavMode = attachBtn(R.id.btnNavMode);
        _title =(TextView中)findViewById(R.id.title);
    }
    专用按钮attachBtn(INT btnId){
        按钮B =(按钮)findViewById(btnId);
        b.setOnClickListener(本);
        返回b;
    }

    布尔haveSel;
    公共无效的onClick(视图v)
    {
        !haveSel = haveSel;
        setVisibilities();
    }
    无效setVisibilities()
    {
        _btnFindRoute.setVisibility(?haveSel View.VISIBLE:View.INVISIBLE);
        _btnUnselect.setVisibility(?haveSel View.VISIBLE:View.INVISIBLE);

        //解决问题
        // _ title.setText(_title.getText());
    }
}
 

解决方案

SurfaceView是唯一的罪魁祸首(当然,这也适用于GLSurfaceView,RSSurfaceView和VideoView,所有这些都继承SurfaceView)。它在与在它上面的其他意见处理暴露许多怪异行为。与View.setVisibility()播放是其中的问题之一。显然,SurfaceView并没有被设计为可与其他视图(尽管官方的文档说,这应该是)使用,但为视频,游戏或OpenGL的东西一个独立的观点。

有关知名度的问题,我已经发现,使用,而不是View.INVISIBLE View.GONE解决。如果你不希望使用不见了,尝试改变的重点,例如(再回到那个有焦点前),或改变其他国家。我们的目标是某种方式唤醒底层UI系统

在短:当一些奇怪的情况发生你的看法和你有一个SurfaceView(或子类)的地方,尝试用别的东西代替它,这样你就不会失去时间寻找你在做什么错了,当你这样做正确的(没有错误的信念)。这样一来,你知道SurfaceView是难辞其咎的,你可以围绕它破解与美丽的意见,小便就可以了无疑虑。

I am trying to show a pair of hidden buttons (using setVisibility(View.VISIBLE), within a RelativeLayout), but it doesn't always work. The button shows OK on a Galaxy Tab 10.1" but not in a smaller tablet (not sure which model), nor on an Android 4.0 emulator.

I randomly discovered that, for a certain TextView t, the following code causes the buttons to become visible:

t.setText(t.getText());
...
button.setVisibility(View.VISIBLE);

t is located in the same RelativeLayout but is not related to the buttons (their locations are independent and non-overlapping).

Edit: In case some Android dev wants to track this down...

I was able to reduce the code to the following layout that exhibits the problem on an Android 4.0.3 emulator but not a Galaxy Tab. I found that I need a SurfaceView or the problem does not occur (for example, change it to TextView and the problem disappears).

<?xml version="1.0" encoding="utf-8"?>
<!-- layout/test.xml -->
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"        
    android:id="@+id/relativeLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <SurfaceView
        android:id="@+id/mapCtrl"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/bottomPanel"
        android:text="Placeholder"
        android:layout_marginTop="18dip" />
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="@string/map_mode_title" />

    <!--=================================================-->
    <!-- Bottom bar: current road name and current speed -->
    <LinearLayout
        android:id="@+id/bottomPanel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#f228"
        android:orientation="horizontal"
        android:textColor="#ffff" >
        <Button
            android:id="@+id/btnNavMode"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginRight="3dip"
            android:textColor="#fff"
            android:text="Switch to\nNav Mode" />
        <RelativeLayout
            android:id="@+id/currentStreetPanel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onClick"
            android:clickable="true"
            android:orientation="vertical" >
            <TextView
                android:id="@+id/currentStreetHdg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:text="Current street"
                android:textColor="#fff"
                android:textSize="10dip" />
            <TextView
                android:id="@+id/currentStreet"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/currentStreetHdg"
                android:layout_marginTop="-8dip"
                android:singleLine="true"
                android:text="Current street"
                android:textColor="#fff"
                android:textSize="30dip" />
        </RelativeLayout>
        <RelativeLayout
            android:id="@+id/RelativeLayout2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="#ff606060"
            android:orientation="vertical" >
            <TextView
                android:id="@+id/yourSpeedHdg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="3dip"
                android:text="Your speed"
                android:textColor="#fff"
                android:textSize="10dip" />
            <TextView
                android:id="@+id/speed"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/yourSpeedHdg"
                android:layout_marginLeft="3dip"
                android:layout_marginTop="-8dip"
                android:text="0"
                android:textColor="#fff"
                android:textSize="30dip" />
            <TextView
                android:id="@+id/speedUnit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@+id/speed"
                android:layout_marginLeft="5dip"
                android:layout_toRightOf="@+id/speed"
                android:text="kph"
                android:textColor="#fff"
                android:textSize="18dip" />
        </RelativeLayout>
    </LinearLayout>

    <!--================-->
    <!-- On-map buttons -->
    <Button
        android:id="@+id/btnClearRoute"
        android:background="#F00"
        android:textColor="#fff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Clear\nroute"/>
    <ZoomControls
        android:id="@+id/zoomControls"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/mapCtrl"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="-25dip"
        android:orientation="horizontal" />
    <Button
        android:id="@+id/btnFindRoute"
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/mapCtrl"
        android:layout_alignParentRight="true"
        android:layout_marginRight="2dip"
        android:layout_marginBottom="65dip"
        android:text="Route to selected location"
        android:textSize="17dip"/>
    <Button
        android:id="@+id/btnUnselect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/btnFindRoute"
        android:layout_alignTop="@+id/btnFindRoute"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="2dip"
        android:text="Unselect" />
    <LinearLayout
        android:id="@+id/showMePanel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnFindRoute"
        android:layout_alignRight="@+id/btnFindRoute"
        android:layout_alignLeft="@+id/btnFindRoute"
        android:padding="4dip"
        android:background="#bbbb"
        android:gravity="center"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Show me..."
            android:textColor="#fff"/>
        <Button
            android:id="@+id/btnShowVehicle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="My car"/>
        <Button
            android:id="@+id/btnShowRoute"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="The route"/>
        <Button
            android:id="@+id/btnShowDestination"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Destination"/>
        <Button
            android:id="@+id/btnShowMap"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="The map"/>
    </LinearLayout>
</RelativeLayout>

The Activity class simply toggles the visibility of the two buttons when any of the buttons are clicked. Again, on some devices it works, on others it does not.

package mentor.simplegps;

import android.app.*;
import android.os.Bundle;
import android.view.*;
import android.widget.*;

public class TestActivity extends Activity implements View.OnClickListener
{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.test);
        boilerplate();
        setVisibilities();
    }

    Button _btnShowMap, _btnShowVehicle, _btnShowRoute, _btnShowDestination;
    Button _btnUnselect, _btnFindRoute, _btnNavMode;
    TextView _title;

    void boilerplate()
    {
        _btnUnselect = attachBtn(R.id.btnUnselect);
        _btnShowMap = attachBtn(R.id.btnShowMap);
        _btnShowVehicle = attachBtn(R.id.btnShowVehicle);
        _btnShowRoute = attachBtn(R.id.btnShowRoute);
        _btnShowDestination = attachBtn(R.id.btnShowDestination);
        _btnFindRoute = attachBtn(R.id.btnFindRoute);
        _btnNavMode = attachBtn(R.id.btnNavMode);
        _title = (TextView)findViewById(R.id.title);
    }
    private Button attachBtn(int btnId) {
        Button b = (Button)findViewById(btnId);
        b.setOnClickListener(this);
        return b;
    }

    boolean haveSel;
    public void onClick(View v)
    {
        haveSel = !haveSel;
        setVisibilities();
    }
    void setVisibilities()
    {
        _btnFindRoute.setVisibility(haveSel ? View.VISIBLE : View.INVISIBLE);
        _btnUnselect.setVisibility (haveSel ? View.VISIBLE : View.INVISIBLE);

        // Fixes the problem
        //_title.setText(_title.getText());
    }
}

解决方案

SurfaceView is the sole culprit (of course, this also applies to GLSurfaceView, RSSurfaceView and VideoView, all of which inherits from SurfaceView). It exposes lots of weird behaviours when dealing with other views on top of it. Playing with View.setVisibility() is one of those issues. Clearly, SurfaceView has not been designed to be used with other views (even though the official doc says it ought to be) but as a standalone view for videos, games or OpenGL stuffs.

For the visibility issue, I've found that using View.GONE instead of View.INVISIBLE resolve it. If you don't want to use GONE, try changing the focus for example (and back to the one that had focus before), or changing other states. The goal is to wake up the underlying UI system somehow.

In short: when something weird happens with your views and you have a SurfaceView (or subclass) somewhere, try replacing it with something else so you don't lose hours searching what you're doing wrong when you're doing it right (and no false beliefs). This way, you know SurfaceView is to blame and you can hack around it with beautiful comments to piss on it without qualms.

这篇关于setVisibility(View.VISIBLE)并不总是有效。想法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆