23

我正在尝试显示一对隐藏按钮(setVisibility(View.VISIBLE)在 a 中使用 , RelativeLayout),但它并不总是有效。该按钮在 Galaxy Tab 10.1" 上显示 OK,但在较小的平板电脑(不确定是哪个型号)和 Android 4.0 模拟器上都没有。

我随机发现,对于某些TextView t情况,以下代码会导致按钮变得可见:

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

t位于相同RelativeLayout但与按钮无关(它们的位置独立且不重叠)。

编辑:如果一些 Android 开发人员想要追踪这个...

我能够将代码简化为以下布局,该布局在 Android 4.0.3 模拟器上出现问题,但在 Galaxy Tab 上没有。发现需要aSurfaceView或者问题没有出现(比如改成TextView问题就消失了)。

<?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>

Activity 类只是在单击任何按钮时切换两个按钮的可见性。同样,在某些设备上它可以工作,而在其他设备上则不能。

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());
    }
}
4

5 回答 5

46

SurfaceView 是唯一的罪魁祸首(当然,这也适用于 GLSurfaceView、RSSurfaceView 和 VideoView,它们都继承自 SurfaceView)。在处理其他视图时,它会暴露出许多奇怪的行为。使用 View.setVisibility() 是这些问题之一。显然,SurfaceView 并未设计为与其他视图一起使用(尽管官方文档说它应该如此),而是作为视频、游戏或 OpenGL 内容的独立视图。

对于可见性问题,我发现使用 View.GONE 而不是 View.INVISIBLE 可以解决它。如果您不想使用 GONE,请尝试更改焦点(并返回之前的焦点),或更改其他状态。目标是以某种方式唤醒底层 UI 系统。

简而言之:当您的视图发生奇怪的事情并且您在某处有一个 SurfaceView(或子类)时,请尝试将其替换为其他内容,这样您就不会浪费时间搜索您做错了什么(并且没有错误的信念)。这样一来,你就知道 SurfaceView 是罪魁祸首,你可以用漂亮的评论绕过它,毫不犹豫地对它撒尿。

于 2012-09-29T19:17:25.337 回答
20

作为记录:我遇到了这个问题,尝试了一堆随机的东西(感谢亚历克斯!),在我的情况下,解决它的方法是在seekBar.requestLayout()拒绝显示的搜索栏上的 setVisible 之后直接做的。

于 2013-02-18T20:58:45.247 回答
5

这是我的解决方案

setAlpha(0)
btnName.setAlpha(0)

适用于所有视图,例如 => Buttons - Images - Texts and ...

于 2017-09-14T07:50:52.063 回答
0

我(很烦人)在 SurfaceView 预览顶部有一个按钮时遇到了类似的困难,不得不将按钮放在相对布局中并使相对布局可见/不可见。对于遇到同样问题的其他人来说,可能值得一试。

...而且我还必须在 findViewById 之后以编程方式调用要被带到的布局:buttonLayout.bringToFront()。

于 2017-05-30T20:53:51.087 回答
0

在我的情况下View.VISIBLE/View.GONE并不总是工作。当我切换到它时,View.VISIBLE/View.INVISIBLE它开始按预期工作。

于 2017-02-08T11:26:12.613 回答