使用2个布局进行相同活动时出错

时间:2016-02-05 11:25:55

标签: android android-layout

我正在尝试将2 layoutsactivity相关联,我已经实现了,但second layout没有出现。

只显示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>

4 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

您的第二个视图未添加到主视图中。

试试这个

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

答案 2 :(得分: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);

}

答案 3 :(得分: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);
}