3

好的,所以我做了一些环顾四周,我知道你应该怎么做,但对我来说,它只是行不通。

我需要能够在 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>

我得到错误:no resource identifier found for attribute 'alpha' in package 'android'

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

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

更新

尽管使用可见性属性有效,但它使我无法单击 ViewGroup。这对我很重要,因为我正在使用 ViewGroup 的 OnTouchListener。

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

我希望这是有道理的。

4

7 回答 7

15

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>
于 2011-03-15T21:41:33.393 回答
3

除非您需要 0 到 1 之间的 alpha 级别,否则我建议,如果您真的想让该项目不可见,请使用setVisibility();

android:visibility="invisible"

我检查了 android:alpha 行,我的 ide 也没有找到它。不过,我猜不出为什么……文档看起来很清楚。

于 2011-03-08T05:54:25.673 回答
3

alpha 属性是 Android 3.0 中的新属性,它不是隐藏视图的最有效方式。使用 View.setVisibility() 或 android:visibility 来实现你想要的。

于 2011-03-17T18:22:37.663 回答
1

您可以通过设置我猜的(背景)颜色来设置 alpha。颜色值的格式可以是#aarrggbb(alpha、red、green、blue)。

于 2011-03-08T07:34:09.907 回答
1

您可以在正确答案中添加以下选项:

animation.setDuration(xxx);

到每个动画实例。这样你的动画会更好看。

于 2012-10-01T20:52:09.620 回答
0

根据您的描述,您应该能够创建一个仅包含相对布局的视图并将 onClickListener 设置为它。这样,您可以将相对布局的可见性设置为不可见,但仍会注册一次点击。

<?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>
于 2011-03-14T19:21:24.407 回答
0

Activity中使用onTouchEvent,然后即使它是“不可见的”,您也可以将触摸事件控制到您的 RelativeLayout 。

于 2011-03-16T17:58:44.160 回答