Android change layout dynamically

后端 未结 4 1601
既然无缘
既然无缘 2020-12-02 17:45

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 th

相关标签:
4条回答
  • 2020-12-02 18:18

    Just use the ViewSwitcher,using this one can switch between any number of layouts within the application without having to do any setContentView.

    0 讨论(0)
  • 2020-12-02 18:21

    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).

    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);
    

    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.

    EDIT: 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"/>
    
    0 讨论(0)
  • 2020-12-02 18:21

    This leaves me to add to adnorid's post, that ViewSwitcher itself is only able to switch between 2 layouts/views. This is simply because it's hardcoded to only 2 layouts/views without deeper meaning, as far as i know. So i built my own MultipleViewSwitcher aka MVS, base on the original ViewSwitcher.java from android source. The only changes that had to be made are:

    1. Provide the MVS with own
      • boolean mFirstTime and
      • int mWhichChild
    2. Correct hardcoded into dynamic handling of an arbitrary number of views in functions
      • addView(...)
      • getNextView()
      • reset()

    This can all be done in MultipleViewSwitcher.java alone and works like a charm.

    I came accross a bunch of stuff like that where the android sources could be made much more powerful with, many times, near-no-brainer-edits. I'd like to know why that is, but that's not for this thread here.

    cheers!

    0 讨论(0)
  • 2020-12-02 18:23

    maybe you could use a postDelayed()-call to execute a runnable that will load a new xml file by calling setContentView(R.xml.anotherxml).

    0 讨论(0)
提交回复
热议问题