23

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

5 に答える 5

46

SurfaceViewが唯一の原因です(もちろん、これはGLSurfaceView、RSSurfaceView、およびVideoViewにも当てはまります。これらはすべてSurfaceViewから継承されます)。その上で他のビューを処理するときに、多くの奇妙な動作が明らかになります。View.setVisibility()で遊ぶことはそれらの問題の1つです。明らかに、SurfaceViewは、他のビューで使用するように設計されていません(公式のドキュメントでは使用する必要があるとされていますが)が、ビデオ、ゲーム、またはOpenGL関連のスタンドアロンビューとして使用するように設計されています。

可視性の問題については、View.INVISIBLEの代わりにView.GONEを使用すると解決することがわかりました。GONEを使用したくない場合は、たとえばフォーカスを変更するか(以前にフォーカスがあったものに戻す)、または他の状態を変更してみてください。目標は、基盤となるUIシステムを何らかの方法でウェイクアップすることです。

つまり、ビューで何か奇妙なことが起こり、SurfaceView(またはサブクラス)がどこかにある場合は、それを別のものに置き換えてみてください。そうすれば、正しく実行しているときに間違っていることを検索する時間を失うことはありません。誤った信念はありません)。このように、SurfaceViewのせいであり、美しいコメントでハックして、気を悪くすることなくそれに腹を立てることができます。

于 2012-09-29T19:17:25.337 に答える
20

記録のために:私はこの問題を抱えていて、たくさんのランダムなものを試しました(Alexに感謝します!)、そして私の場合、それを解決したseekBar.requestLayout()のは、表示を拒否していた非常にシークバーのsetVisibleの直後でした。

于 2013-02-18T20:58:45.247 に答える
5

これが私の解決策です

setAlpha(0)
btnName.setAlpha(0)

=>ボタン-画像-テキストや...などのすべてのビューで機能しています。

于 2017-09-14T07:50:52.063 に答える
0

私の場合、View.VISIBLE/View.GONEいつもうまくいっていませんでした。トグルを切り替えるとView.VISIBLE/View.INVISIBLE、意図したとおりに機能し始めました。

于 2017-02-08T11:26:12.613 に答える
0

私は(迷惑なことに)SurfaceViewプレビューの上にボタンを配置するのと同様の問題を抱えており、ボタンをRelativeLayoutに配置して、RelativeLayoutをVISIBLE/INVISIBLEにする必要がありました。同じ問題を抱えている他の人にとっては、一撃の価値があるかもしれません。

...また、findViewByIdの直後に次の場所から移動するレイアウトをプログラムで呼び出す必要がありました:buttonLayout.bringToFront()。

于 2017-05-30T20:53:51.087 に答える