安卓 -- 隐藏视图

3

好的,我已经查看了一些资料,了解到你应该如何做,但对于我来说,它并没有起作用。

我需要能够在XML和代码中设置RelativeLayout的透明度。对于我的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文档,我应该能够在任何View对象上调用setAlpha(double)方法,但是当我尝试在RelativeLayout上进行调用时,它告诉我该方法未定义于此对象。为什么我不能控制Android中RelativeLayout对象的透明度呢?我是否漏掉了什么?谢谢!
更新:虽然使用visibility属性可以解决问题,但是它会使ViewGroup无法被点击。这对我很重要,因为我正在利用ViewGroup的OnTouchListener。我的目标是有一个带有媒体控件的层,一开始是隐藏的。当用户点击屏幕的任何位置时,我希望控件淡入,再次点击屏幕时,我希望控件淡出。我已经实现了这部分功能。我使用一个覆盖整个应用程序的viewgroup,并附加了一个OnTouchListener来确定它是否被触摸。我的问题是,在动画运行以淡出控件后,它们会重新出现。如果我使用@Hydrangea的建议,我可以让它淡出并立即变为不可见。这给了我想要的效果,但是ViewGroup无法被点击,用户无法让控件回来(或者首先消失,具体取决于我们决定做什么)。希望我的解释清晰明了。

1
Brian Griffey回答中的关键是在动画上使用setFillAfter(true),而不是将视图设置为INVISIBLEGONE。这将使视图在动画完成运行后呈现透明状态。 - Yoni Samlan
7个回答

15

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

3

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


3
除非你需要0到1之间的alpha级别,如果你真的想让这个项目不可见,我建议你使用setVisibility();。请参考setVisibility();了解更多详情。
android:visibility="invisible"

我查看了android:alpha这一行,但我的IDE也找不到它。虽然文档似乎很清晰,但我无法猜测原因。


我相信这可能会有效。我会试一试,但是我很好奇为什么文档定义了一个alpha属性,但是你却不能使用它。 - user131441
1
刚试了一下,它可以工作,但不符合我的需求。我需要能够将ViewGroup的alpha设置为0(因此它被隐藏),但仍然能够单击同一个ViewGroup。如果我使用可见性选项,当它被隐藏时,它是无法点击的。 - user131441

1

你可以通过设置(背景)颜色来设置alpha值。颜色值可以采用#aarrggbb的格式(alpha, 红色, 绿色, 蓝色)。


如果你看了我的更新,这个可能会起作用...但我想它可能有点笨重。然而,如果我找不到其他的东西,这就是我要尝试的。 - user131441

1
您可以在正确答案中添加以下选项:
animation.setDuration(xxx);

给每个动画实例添加这样的效果,这样你的动画看起来会更好。

0
Activity中使用onTouchEvent,即可获取触摸事件并控制你的RelativeLayout,即使它是“不可见”的。

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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接