Android动态更改布局

Android动态更改布局,android,android-activity,Android,Android Activity,我的情况是,我想在3秒钟后更改活动的布局。我能做到吗? 例如,应用程序以运行3秒的splashscreen启动,然后自动将布局切换到应用程序的第一个屏幕布局。这应该发生在同一个活动中,有什么想法吗 谢谢也许您可以使用postDelayed()-调用来执行runnable,它将通过调用setContentView(R.xml.anotherxml)来加载新的xml文件。只需使用ViewSwitcher,使用此选项可以在应用程序中任意数量的布局之间切换,而无需执行任何setContentView。我

我的情况是,我想在3秒钟后更改活动的布局。我能做到吗? 例如,应用程序以运行3秒的splashscreen启动,然后自动将布局切换到应用程序的第一个屏幕布局。这应该发生在同一个活动中,有什么想法吗


谢谢

也许您可以使用postDelayed()-调用来执行runnable,它将通过调用setContentView(R.xml.anotherxml)来加载新的xml文件。

只需使用ViewSwitcher,使用此选项可以在应用程序中任意数量的布局之间切换,而无需执行任何setContentView。

我只使用了一个xml布局。我只是在它里面放了一个额外的RelativeLayout,它代表我的介绍屏幕,然后我在上面使用淡出动画,然后调用.setVisibility(View.GONE)

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

<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>
您可以将startAnimation()和setVisibility放在runnable中,并使用markus提到的postDelayed()在3秒钟后运行。考虑一下做一些工作,虽然这个布局布局是在屏幕上,所以它不只是一个3秒的延迟为用户。也许可以检查应用程序的运行版本是否为当前版本

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

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"/>

这让我在adnorid的帖子中补充说,ViewSwitcher本身只能在2个布局/视图之间切换。这仅仅是因为据我所知,它只硬编码为2个布局/视图,没有更深的含义。因此,我构建了自己的MultipleViewSwitcher又名MVS,它基于android源代码中最初的ViewSwitcher.java。必须进行的唯一更改是:

  • 为MVS提供自己的
    • 布尔mFirstTime和
    • int mWhichChild
  • 正确的硬编码为动态处理函数中任意数量的视图
    • addView(…)
    • getNextView()
    • 重置()
  • 这一切都可以在MultipleViewSwitcher.java中单独完成,并且工作起来很有魅力

    我遇到了一大堆类似的东西,在这些东西中,android源代码可以通过很多次近乎无脑的编辑变得更加强大。我想知道这是为什么,但这不是这个线程在这里


    干杯

    R.anim.
    中的
    .anim.
    在哪里?我得到一个
    动画无法解析或不是字段
    @w0rldart您需要将fadeout.xml添加到
    /res/anim/
    文件夹,请参见我的编辑。
    <?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"/>