2016-02-05 42 views
0

我想链接2 layouts到相同的activity,我已经实现了它,但没有出现second layout使用2个布局进行相同的活动时出错

只显示一个white blank screen直到出现activity_main layout

protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     final LayoutInflater factory = getLayoutInflater(); 
     final RelativeLayout second = (RelativeLayout) factory.inflate(R.layout.welcome_layout, null); 
     second.setVisibility(View.VISIBLE); 
     loading_img = (ImageView)second.findViewById(R.id.loading_view); 
     final Animation animatable = AnimationUtils.loadAnimation(this, R.anim.welcome_screen_anim); 
     loading_img.setAnimation(animatable); 
     long i; 
     for(i = 0; i < 100000000; i++); 
     second.setVisibility(View.GONE); 
} 

welcome_layout

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent" 
    android:theme="@android:style/Theme.NoTitleBar" 
    android:background="@color/MyBlue" 
    > 
    <ImageView 
     android:layout_width="300dp" 
     android:layout_height="300dp" 
     android:src="@drawable/my_launcher" 
     android:paddingTop="60dp" 
     android:id="@+id/imageView" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" /> 

    <ImageView 
     android:layout_width="75dp" 
     android:layout_height="75dp" 
     android:src="@drawable/loading" 
     android:layout_marginBottom="43dp" 
     android:id="@+id/loading_view" 

     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Welcome" 
     android:id="@+id/welcome_message" 
     android:focusable="false" 
     android:textColor="#010101" 
     android:textSize="@android:dimen/app_icon_size" 
     android:layout_below="@+id/imageView" 
     android:layout_centerHorizontal="true" /> 

</RelativeLayout> 

activity_main

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    android:id="@+id/first" 
    tools:context="andy.propaganda.MainActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 

    <include layout="@layout/content_main" /> 

</android.support.design.widget.CoordinatorLayout> 
+0

粘贴您的logcat的错误。 – AKSiddique

+0

它没有显示任何错误,它只是空白的,直到它消失 – andrew

+2

提示:您不需要投射到'RelativeLayout'(除View以外的其他任何东西)。使用'final查看第二个= factory.inflate(R.layout.welcome_layout,null);' –

回答

0

你的第二视图是不添加到主视图。

试试这个

first = (RelativeLayout)findViewById(R.id.ll_first); 
first.addView(second); 
+0

可以给你更多的解释,是第一个视图组?正如我试过它和应用程序停止工作 – andrew

+0

你可以发布你的布局文件。 – Simar

+0

已添加它们 – andrew

0

我不知道, 也许你忘了第二视图添加到主视图。你可以这样做:

您activity_main.xml中应该是这样的

<FrameLayout 
    android:id="@+id/mainLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    // Your code here 

</FrameLayout> 

现在在MainActivity.java:

protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    FrameLayout mainLayout = (FrameLayout) findViewById(R.id.mainLayout); 
    final LayoutInflater factory = getLayoutInflater(); 
    final RelativeLayout second = (RelativeLayout) factory.inflate(R.layout.welcome_layout, null); 
    mainLayout.addView(second); 
    second.setVisibility(View.VISIBLE); 
    loading_img = (ImageView)second.findViewById(R.id.loading_view); 
    final Animation animatable = AnimationUtils.loadAnimation(this, R.anim.welcome_screen_anim); 
    loading_img.setAnimation(animatable); 
    long i; 
    for(i = 0; i < 100000000; i++); 
    second.setVisibility(View.GONE); 

}

+0

我不认为这是可能的,因为我使用frameLayout用于其他目的,两种布局都有不同的主题 – andrew

+0

如果有帮助,您可以使用RelativeLayout而不是FrameLayout。 –

+0

没有为我工作,我需要改变很多片段 – andrew

0

尝试这样的事情,

例如:

protected void onCreate(Bundle savedInstanceState) { 
Super.onCreate(savedInstanceState); 
    LinearLayout layoutMain = new LinearLayout(this); 
    layoutMain.setOrientation(LinearLayout.HORIZONTAL); 
    setContentView(layoutMain); 
    LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    RelativeLayout layoutLeft = (RelativeLayout) inflate.inflate(
     R.layout.main, null); 
    RelativeLayout layoutRight = (RelativeLayout) inflate.inflate(
     R.layout.row, null); 
} 
1

其实是有,你想要达到什么样的一个ViewGroup中,叫ViewAnimator

activity_mail.xml

<!-- Welcome layout --> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:theme="@android:style/Theme.NoTitleBar" 
     android:background="@color/MyBlue"> 

     <ImageView 
      android:layout_width="300dp" 
      android:layout_height="300dp" 
      android:src="@drawable/my_launcher" 
      android:paddingTop="60dp" 
      android:id="@+id/imageView" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" /> 

     <ImageView 
      android:layout_width="75dp" 
      android:layout_height="75dp" 
      android:src="@drawable/loading" 
      android:layout_marginBottom="43dp" 
      android:id="@+id/loading_view" 

      android:layout_alignParentBottom="true" 
      android:layout_centerHorizontal="true" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:text="Welcome" 
      android:id="@+id/welcome_message" 
      android:focusable="false" 
      android:textColor="#010101" 
      android:textSize="@android:dimen/app_icon_size" 
      android:layout_below="@+id/imageView" 
      android:layout_centerHorizontal="true" /> 

    </RelativeLayout> 

    <!-- Activity main content --> 

    <android.support.design.widget.CoordinatorLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     android:id="@+id/first" 
     tools:context="andy.propaganda.MainActivity"> 

     <android.support.design.widget.AppBarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:theme="@style/AppTheme.AppBarOverlay"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="?attr/colorPrimary" 
       app:popupTheme="@style/AppTheme.PopupOverlay" /> 

     </android.support.design.widget.AppBarLayout> 

     <include layout="@layout/content_main" /> 

    </android.support.design.widget.CoordinatorLayout> 

</ViewAnimator> 

然后

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    final ViewAnimator animator = (ViewAnimator) findViewById(R.id.animator); 

    // This is not a good way to wait. This will block UI and not draw 
    //anything until done. Use postDelayed() instead 
    //long i; for(i = 0; i < 100000000; i++); 
    animator.postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      animator.setDisplayedChild(1); 
     } 
    }, 2000); 
相关问题