【问题标题】:Change Layouts Dynamically in 1 Activity在 1 个活动中动态更改布局
【发布时间】:2016-02-10 07:53:27
【问题描述】:

我对此Post 有相同的要求,我遵循FoamyGuy's Answer,但我没有得到显示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);
    }

帮助?

【问题讨论】:

    标签: java android layout


    【解决方案1】:

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

    webview.setVisibility (View.VISIBLE);

    而不是:

    webview.setVisibility(1);

    PS:使用 View.GONE 使视图不可见,使用 View.VISIBLE 使视图可见:)。 只是因为你在 MainActivity 中做了相反的事情然后你描述了。

    【讨论】:

      【解决方案2】:

      你的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>
      

      第二活动

      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>
      

      【讨论】:

      • 我希望 introLayout 先显示然后隐藏,这就是为什么 View.GONE 在那一点
      • 不,您将“@+id/introLayout”设置为默认隐藏。
      • @Rafael Reyes 我写了一些代码,也许你会发现一些有用的东西。
      • 感谢您的回答!但它可以在一项活动中完成?
      • 是的,它可以,但我决定向您展示这段代码,您可以稍后将它用于启动屏幕,用于您的徽标或类似的东西。
      【解决方案3】:

      首先,更改简介的删除。在您的布局中将其设置在 WebView 下方。 WebView 和 intro 不需要设置标签可见性。所以......在你的 onCreate() 应用这个:

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

      2000 = 2 秒。随心所欲地改变它。

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

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

      【讨论】:

        猜你喜欢
        • 2015-08-30
        • 1970-01-01
        • 2015-01-22
        • 1970-01-01
        • 2015-06-26
        • 2023-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多