Android 动态更改布局 [英] Android change layout dynamically

查看:18
本文介绍了Android 动态更改布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 3 秒后更改活动布局.我可以这样做吗?例如,应用程序以启动画面启动,它将运行 3 秒,然后自动将布局切换到应用程序的第一个屏幕布局.这应该发生在同一个活动中,有什么想法吗?

I have situation where I want to change layout of activity after 3 seconds. Ho can I do that? For example application starts with splashscreen that will run for 3 seconds then automatically switch layout to application's first screen layout. This should happen in the same activity, any ideas?

谢谢

推荐答案

我只使用了一个 xml 布局.我只是在其中添加了一个额外的 RelativeLayout 来代表我的介绍屏幕,然后在其上使用淡入淡出动画,然后调用 .setVisibility(View.GONE).

I did this using only one xml layout. I just put an extra RelativeLayout in it that represents my intro screen then I use a fadeOut Animation on it and then call .setVisibility(View.GONE).

这是我的 main.xml 布局文件的一部分

This is part of my main.xml layout file

<RelativeLayout
        android:id="@+id/introLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
      android:background="#FFFFFF"
    >

        <ImageView
        android:id="@+id/logoImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/logo">
        </ImageView>
    </RelativeLayout>

然后在我的活动中我有这个:

Then inside my activity I have this:

introLayout = (RelativeLayout) findViewById(R.id.introLayout);
Animation fadeOutAnim = AnimationUtils.loadAnimation(MyActivity.this, R.anim.fadeout);
introLayout.startAnimation(fadeOutAnim);
introLayout.setVisibility(View.GONE);

您可以通过将 startAnimation() 和 setVisibility 放在 runnable 中并使用 postDelayed() 来在 3 秒后运行,如markus 所述.当这个介绍布局在屏幕上时,请考虑做一些工作,所以它不仅仅是用户的 3 秒延迟.也许检查应用程序的运行版本是否是当前版本.

You could make this run after 3 seconds by putting the startAnimation(), and the setVisibility inside of a runnable and using postDelayed() as markus mentioned. Do consider doing some work while this intro layout is on the screen though, so its not just a 3 second delay for the user. Perhaps check to see if the running version of the application is the current version or not.

您需要将 fadout.xml 文件添加到 /res/anim/(如果它不存在,则创建 anim 目录).这是一个例子.

You'll need to add the fadout.xml file to /res/anim/ (create the anim directory if it doesn't exist). Here is an example.

fadeout.xml

<?xml version="1.0" encoding="utf-8"?>

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
       android:interpolator="@android:anim/accelerate_interpolator"
       android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="700" 
       android:fillAfter="true"/>

这篇关于Android 动态更改布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆