Android — Hiding Views

∥☆過路亽.° 提交于 2019-12-04 14:32:12

You'll want to use a alpha animation to fade things in and out. This will maintain your touch events for your layouts. Here's an example

public class Main extends Activity {
/** Called when the activity is first created. */

private boolean mShowing = false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    findViewById(R.id.textview).setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            if(mShowing){
            Animation animation = new AlphaAnimation(1.0f, 0.0f);
            animation.setFillAfter(true);
            arg0.startAnimation(animation);
            } else {
                Animation animation = new AlphaAnimation(0.0f, 1.0f);
                animation.setFillAfter(true);
                arg0.startAnimation(animation);
            }

            mShowing = !mShowing;
        }

    });
}

}

Here's the accompanying xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:id="@+id/textview"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    android:clickable="true"
    />
</LinearLayout>

Unless you need levels of alpha between 0 and 1, I'd suggest, if you truly want to make this item invisible, to use setVisibility();

android:visibility="invisible"

I checked out the android:alpha line, and my ide doesn't find it either. I can't guess why, though... the documentation seems pretty clear.

The alpha property is new in Android 3.0, and it's not the most efficient way to hide a view. Use View.setVisibility() or android:visibility to achieve what you want.

You can set alpha by setting the (background) color i guess. Color values can be in the format of #aarrggbb (alpha, red, green, blue).

noni

You can add to the right answer the following option:

animation.setDuration(xxx);

To each animation instance. In this way your animation will look better.

Based on your discription, you should be able to create a view that contains only the relative layout and have the onClickListener set to it. This way you can set the visibility of the relative layout to invisible, but still register a click.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/clickable_layout"
    android:layout_height="match_parent"
    android:layout_width="match_parent" >
    <RelativeLayout
        android:id="@+id/player_controls"
        android:layout_height="match_parent"
        android:layout_width="match_parent" >
        <RelativeLayout
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:id="@+id/player_controls_touch_me"
        >
        </RelativeLayout>
    </RelativeLayout>
</FrameLayout>

Use onTouchEvent in Activity, and then you could get touch event to control to your RelativeLayout even if it is "invisible".

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