简体   繁体   中英

Change Layouts Dynamically in 1 Activity

I have the same request of this Post and I followed the FoamyGuy's Answer but I don't get the introLayout with the ImageView displaying.

I just want to show my introLayout first and then change it to my WebView.

here is my main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/introLayout"
                    android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:background="@color/white"
                    android:visibility="visible"
    >

        <ImageView android:layout_width="wrap_content"
                   android:contentDescription="splash screen"
                   android:id="@+id/splash"
                   android:src="@drawable/splash"
                   android:layout_height="wrap_content"
                   android:scaleType="centerInside"/>
    </RelativeLayout>

    <WebView
            android:id="@+id/browser"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="invisible"
    />

</LinearLayout>

in MainActivity.java

@Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            RelativeLayout introLayout = (RelativeLayout) findViewById(R.id.introLayout);
            introLayout.setVisibility(View.GONE);
            webview = (WebView)findViewById(R.id.browser);
            webview.setVisibility(1);
    }

Help?

Your introLayout is set View.GONE , change to

introLayout.setVisibility(View.VISIBLE); 

You need to make some logic in your code, when you will hide this "@+id/introLayout" and then show your WebView .

Ok, here is how would i do that if you dont need that layout after few seconds:

ActivityMain

public class Splash extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    Thread timer = new Thread(){
        public void run(){
            try{
                sleep(3000);
            }catch(InterruptedException e){
                e.printStackTrace();
            }finally{
                Intent startSpalsh = new Intent("com.yourpackagename.secondactivity");
                startActivity(startSpalsh);

            }
        }
    };
    timer.start();

}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    finish();
   }


}

splash.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/introLayout"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:gravity="center"
   android:background="@color/white"
   android:visibility="visible">

   <ImageView 
      android:layout_width="wrap_content"
      android:contentDescription="splash screen"
      android:id="@+id/splash"
      android:src="@drawable/splash"
      android:layout_height="wrap_content"
      android:scaleType="centerInside"/>
</RelativeLayout>

SecondActivity

public class SecondActivity extends AppCompatActivity  {

WebView webView;
TextView txtChk;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);

    webview = (WebView)findViewById(R.id.browser);
}

second.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">


 <WebView
        android:id="@+id/browser"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="invisible" />  

  </LinearLayout>

I don't know if it would make any difference but you could try :

webview.setVisibility (View.VISIBLE);

Instead of :

webview.setVisibility (1);

PS: With View.GONE you make the View not visible and with View.VISIBLE you make the View visible :). Just because you did the opposite thing in your MainActivity then you discribed.

First, change de ubication of intro. Set it below of WebView in your layout. WebView and intro don't need to set tag visibility. So... In your onCreate() apply this:

new Handler().postDelayed(new Runnable() { @Override public void run() { introLayout.setVisibility(View.GONE); } }, 2000);

2000 = 2 seconds. Change this like you want.

This code will be later that your initialize WebView and IntroLayout.

Change the LinearLayout by RelativeLayout in your layout how principal container.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM