Android – 隐藏视图

好吧,所以我做了一些环顾四周,我看到你是如何支持的,但对我来说,它只是不起作用.

我需要能够在XML和代码中设置RelativeLayout的alpha.对于我的XML,我有以下内容

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

我收到错误:在’android’包中找不到属性’alpha’的资源标识符

此外,基于Android文档,我应该能够在任何View对象上调用setAlpha(double),但是当我尝试在RelativeLayout上进行调用时,它告诉我没有为此对象定义此方法.

为什么我无法在Android中控制RelativeLayout对象的Alpha透明度?我错过了什么吗?谢谢!

更新

虽然使用visibility属性可以工作,但它阻止我能够单击ViewGroup.这对我很重要,因为我正在使用ViewGroup的OnTouchListener.

我想要做的是拥有一个带有媒体控件的图层,最初是隐藏的.当用户点击屏幕上的任何内容时,我希望控件淡入,当他们再次点击屏幕时,我希望控件淡出.我有这个部分已经工作了.我正在使用一个视图组,该视图组位于我的整个应用程序之上,并附带一个OnTouchListener,可以确定它是否已被触及.我的问题是,在动画运行淡出控件后,它们会重新出现.如果我使用@Hydrangea建议,我可以让它淡出并立即变得不可见.这给了我想要的效果,但是ViewGroup是不可点击的,用户无法让控件返回(或者消失,取决于我们先决定做什么).

我希望这是有道理的.

解决方法:

你会想要使用alpha动画来淡入淡出.这将保持您的布局的触摸事件.这是一个例子

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;
        }

    });
}

}

这是随附的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>
上一篇:android中ViewGroup的addViewInLayout()的行为


下一篇:如何在Android中创建自定义gridview(如矩阵结构)