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

Java 在1活动中动态更改布局,java,android,layout,Java,Android,Layout,我对此有相同的要求,我遵循了,但我没有得到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=

我对此有相同的要求,我遵循了,但我没有得到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>

帮助?

您的
introLayout
已设置为
视图。消失了
,更改为

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

您的
introLayout
已设置为
视图。消失了
,更改为

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>
<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>
second.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>
<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:With View.GONE使视图不可见,With View.View使视图可见:)。
就因为你在主要活动中做了相反的事情,然后你描述了。

我不知道这是否会有什么不同,但你可以试试:

webview.setVisibility(View.VISIBLE)

而不是:

webview.setVisibility(1)

PS:With View.GONE使视图不可见,With View.View使视图可见:)。
仅仅因为你在主要活动中做了相反的事情,然后你就进行了描述。

首先,改变介绍的用法。将其设置在布局中WebView的下方。WebView和intro不需要设置标记可见性。所以在onCreate()中,应用以下内容:

new Handler().postDelayed(new Runnable() { @Override public void run() { introLayout.setVisibility(View.GONE); } }, 2000);
2000=2秒。你想怎么改就怎么改

此代码将在稍后初始化WebView和IntroLayout


通过布局容器中的RelativeLayout更改LinearLayout

首先,改变导入的去泛化。将其设置在布局中WebView的下方。WebView和intro不需要设置标记可见性。所以在onCreate()中,应用以下内容:

new Handler().postDelayed(new Runnable() { @Override public void run() { introLayout.setVisibility(View.GONE); } }, 2000);
2000=2秒。你想怎么改就怎么改

此代码将在稍后初始化WebView和IntroLayout


通过布局容器中的RelativeLayout更改LinearLayout

我希望introLayout先显示,然后隐藏,这就是为什么View.GONE在这一点上不存在不,您将您的“@+id/introLayout”设置为默认隐藏。@Rafael Reyes我写了一些代码,也许您会发现一些有用的东西。谢谢您的回答!但它可以在一个单独的活动中完成?是的,可以,但我决定向您展示此代码,您可以稍后将其用于启动屏幕,用于徽标或类似的内容。我希望introLayout先显示,然后隐藏,这就是为什么View.GONE在那一点上不,您设置了“@+id/introLayout”默认情况下隐藏。@Rafael Reyes我写了一些代码,也许你会发现一些有用的东西。谢谢你的回答!但它可以在一个单独的活动中完成?是的,它可以,但我决定向您展示此代码,您可以稍后将其用于启动屏幕,用于您的徽标或类似的内容。