简体   繁体   English

在1个活动中动态更改布局

[英]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. 我对这篇文章有相同的要求,但我遵循了FoamyGuy的回答,但没有获得显示ImageView的introLayout。

I just want to show my introLayout first and then change it to my WebView. 我只想先显示我的introLayout,然后将其更改为WebView。

here is my main.xml 这是我的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 在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设置为View.GONE ,更改为

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 . 当您要隐藏此“ @ + id / introLayout”然后显示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 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 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); webview.setVisibility(View.VISIBLE);

Instead of : 代替 :

webview.setVisibility (1); webview.setVisibility(1);

PS: With View.GONE you make the View not visible and with View.VISIBLE you make the View visible :). PS:使用View.GONE可以使视图不可见,而使用View.VISIBLE可以使视图可见:)。 Just because you did the opposite thing in your MainActivity then you discribed. 只是因为您在MainActivity中做了相反的事情,所以您进行了描述。

First, change de ubication of intro. 首先,更改介绍的内容。 Set it below of WebView in your layout. 在布局中的WebView下方进行设置。 WebView and intro don't need to set tag visibility. WebView和简介不需要设置标签可见性。 So... In your onCreate() apply this: 所以...在您的onCreate()中应用以下代码:

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

2000 = 2 seconds. 2000 = 2秒。 Change this like you want. 根据需要更改此设置。

This code will be later that your initialize WebView and IntroLayout. 此代码将在以后初始化WebView和IntroLayout时使用。

Change the LinearLayout by RelativeLayout in your layout how principal container. 通过您的布局中的RelativeLayout更改LinearLayout如何成为主体容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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