繁体   English   中英

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

[英]Change Layouts Dynamically in 1 Activity

我对这篇文章有相同的要求,但我遵循了FoamyGuy的回答,但没有获得显示ImageView的introLayout。

我只想先显示我的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);
    }

救命?

您的introLayout设置为View.GONE ,更改为

introLayout.setVisibility(View.VISIBLE); 

当您要隐藏此“ @ + id / introLayout”然后显示WebView时,需要在代码中进行一些逻辑选择。

好的,如果您几秒钟后不需要该布局,这是我该怎么做:

活动主

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>

第二活动

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>

我不知道这是否会有所作为,但您可以尝试:

webview.setVisibility(View.VISIBLE);

代替 :

webview.setVisibility(1);

PS:使用View.GONE可以使视图不可见,而使用View.VISIBLE可以使视图可见:)。 只是因为您在MainActivity中做了相反的事情,所以您进行了描述。

首先,更改介绍的内容。 在布局中的WebView下方进行设置。 WebView和简介不需要设置标签可见性。 所以...在您的onCreate()中应用以下代码:

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

2000 = 2秒。 根据需要更改此设置。

此代码将在以后初始化WebView和IntroLayout时使用。

通过您的布局中的RelativeLayout更改LinearLayout如何成为主体容器。

暂无
暂无

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

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