Android——隐藏视图

发布于 2024-10-21 01:53:18 字数 1303 浏览 2 评论 0原文

好吧,我环顾四周,我明白了你应该如何做,但对我来说,这是行不通的。

我需要能够在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

红衣飘飘貌似仙 2024-10-28 01:53:19

您需要使用 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>

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>
鲜血染红嫁衣 2024-10-28 01:53:19

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

android:visibility="invisible"

我检查了 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();

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.

沫离伤花 2024-10-28 01:53:19

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.

新雨望断虹 2024-10-28 01:53:19

我猜你可以通过设置(背景)颜色来设置 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).

请叫√我孤独 2024-10-28 01:53:19

您可以将以下选项添加到正确的答案中:

animation.setDuration(xxx);

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

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.

愛上了 2024-10-28 01:53:19

根据您的描述,您应该能够创建一个仅包含相对布局的视图,并将 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>

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>
感受沵的脚步 2024-10-28 01:53:19

使用 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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文