2016-02-09 130 views
0

我有這個Post的相同請求,我跟着FoamyGuy's Answer,但我沒有看到帶有ImageView的introLayout顯示。動態地在1個活動中更改佈局

我只想先顯示我的introLayout,然後將其更改爲我的WebView。

,這裏是我的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> 

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

幫助?

回答

1

introLayout設置View.GONE,改變

introLayout.setVisibility(View.VISIBLE); 

你需要做一些邏輯代碼,當你將這個隱藏「@ + ID/introLayout」,然後顯示您的WebView

好,這裏是如何,我會做,如果你不需要幾秒鐘後,該佈局:

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> 
+0

我希望introLayout先顯示然後隱藏,那就是爲什麼View.GONE在那一點 –

+1

不,你設置你的「@ + id/introLayout」默認是隱藏的。 – Stanojkovic

+0

你能寫我的代碼的例子嗎? –

1

我不知道是否會作出任何差別,但你可以嘗試:

webview.setVisibility(View.VISIBLE);

而不是:

webview.setVisibility(1);

PS:隨着View.GONE你使視圖不可見,並與View.VISIBLE你讓視圖可見:)。 只因爲你在MainActivity中做了相反的事情,那麼你就是這樣描述的。

0

首先,變更介紹介紹。在佈局中將其設置在WebView的下方。 WebView和簡介不需要設置標籤可見性。所以...在你的onCreate()申請這個:

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

2000 = 2秒。改變你想要的。

此代碼稍後將初始化WebView和IntroLayout。

在您的佈局中將RelativeLayout更改爲LinearLayout主體容器。