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>
错误: no resourceidentifier found for attribute 'alpha' in package 'android'
另外,根据 Android 文档,我应该能够调用 setAlpha(double)
在任何 View 对象上,但是当我尝试在relativelayout上进行该调用时,它告诉我没有为此对象定义此方法。
为什么我无法控制 Android 中的relativelayout对象的alpha透明度?我错过了什么吗?谢谢!
更新
虽然使用可见性属性有效,但它阻止我单击 ViewGroup。这对我来说很重要,因为我正在利用 ViewGroup 的 OnTouchListener。
我想做的是有一个带有媒体控件的图层,最初是隐藏的。当用户点击屏幕上的任何位置时,我希望控件淡入,当他们再次点击屏幕时,我希望控件淡出。我已经有这部分工作了。我使用的视图组位于整个应用程序之上,并附加了 OnTouchListener,可以确定它是否已被触摸。我的问题是,在动画运行以淡出控件后,它们会重新出现。如果我使用@Hydrangea 建议,我可以让它淡出并立即变得不可见。这给了我想要的效果,但是 ViewGroup 是不可点击的,用户无法让控件返回(或消失,取决于我们决定首先做什么)。
我希望这是有道理的。
Okay, so I've done some looking around and I see how you are SUPPOSED to do it, but for me, it is just not working.
I need to be able to set the alpha of a RelativeLayout both in XML and in code. For my XML, I have the following
<?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>
I get the error: no resource identifier found for attribute 'alpha' in package 'android'
Also, based on the Android documentation, I should be able to call setAlpha(double)
on any View object, but when I try to make that call on a RelativeLayout it tells me that this method is not defined for this object.
Why am I not able to control the alpha transparency for a RelativeLayout object in Android? Am i missing something? Thanks!
Update
Although using the visibility property works, it prevents me from be able to click on the ViewGroup. This is important for me because I am utilizing the OnTouchListener of the ViewGroup.
What I am trying to do is to have a layer with media controls, initially hidden. when the user taps anywere on the screen, I want the controls to fade in and when they tap the screen again I want the controls to fade out. I have this part already working. I am using a viewgroup that sits over-top my entire application with an OnTouchListener attached that can determine if it has or hasn't been touched. My problem is that after the animation runs to fade out the controls, they re-appear. If I use @Hydrangea suggestion, I can have it fade out and immediately made invisible. This gives me the desired effect, but then the ViewGroup is unclickable and the user cannot get the controls to come back (or go away, depending on what we decide to do first).
I hope this makes sense.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您需要使用 Alpha 动画来淡入和淡出内容。这将为您的布局保留触摸事件。这是一个示例
}
这是随附的 xml
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
}
Here's the accompanying xml
除非您需要 0 到 1 之间的 alpha 级别,否则我建议,如果您确实想让该项目不可见,请使用 setVisibility();
我检查了 android:alpha 行,我的 ide 也没有找到它。不过,我无法猜测为什么......文档似乎很清楚。
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();
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.
alpha 属性是 Android 3.0 中的新属性,它不是隐藏视图的最有效方法。使用 View.setVisibility() 或 android:visibility 来实现你想要的。
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.
我猜你可以通过设置(背景)颜色来设置 alpha。颜色值的格式可以是#aarrggbb(alpha、red、green、blue)。
You can set alpha by setting the (background) color i guess. Color values can be in the format of #aarrggbb (alpha, red, green, blue).
您可以将以下选项添加到正确的答案中:
到每个动画实例。这样你的动画就会看起来更好。
You can add to the right answer the following option:
To each animation instance. In this way your animation will look better.
根据您的描述,您应该能够创建一个仅包含相对布局的视图,并将 onClickListener 设置为它。通过这种方式,您可以将相对布局的可见性设置为不可见,但仍然记录单击。
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.
使用 onTouchEvent< /a> 在 Activity 中,然后你即使它是“不可见的”,也可以获取触摸事件来控制您的RelativeLayout。
Use onTouchEvent in Activity, and then you could get touch event to control to your RelativeLayout even if it is "invisible".