有没有更好的方法可以在Android中显示不同的布局?

时间:2013-11-21 01:18:51

标签: android android-layout layout crash

我正在尝试制作具有多种布局的应用。是否有一种有效的方式来显示多个布局?我的应用程序有按钮,每个按钮导致不同的布局。因此,任何人都可以给我一些关于使用什么的想法吗?我14岁,我是android的新手。我在java中有适度的背景,这就是全部。谢谢你的帮助!这是我的主要活动......

package com.example.submenus;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
    public void onBackPressed()  {
        setContentView(R.layout.activity_main);


    }

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

        // This is for Audi A4
        Button button = (Button) findViewById(R.id.a4button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            setContentView(R.layout.audi);
            }

        }   ); 

        // This is for Audi A6
        Button button1 = (Button) findViewById(R.id.a6button);
    button1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            setContentView(R.layout.audia6);
             }

        } ); 

        // This is for Audi Q5
         Button button2 = (Button) findViewById(R.id.q5button);
    button2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            setContentView(R.layout.audiq5);

            }
    } ); 

        // This is for Audi R8
         Button button3 = (Button) findViewById(R.id.r8button);
    button3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            setContentView(R.layout.audir8);

        }
    } );


    }
}

再次,只是想说这是一个有主菜单的应用程序。在该菜单中,我有多个按钮,相应地导致不同的布局。该应用程序工作正常,直到我点击后退按钮然后单击一个按钮。让我重新说一下......我点击一个按钮,它将我带到正确的布局。然后,我可以点击后退按钮,它将布局带回主菜单,就像它应该的那样。然后,如果我点击另一个按钮,该应用程序会冻结并崩溃。有没有办法来解决这个问题?谢谢你的帮助!

2 个答案:

答案 0 :(得分:0)

这应该可以解决您的错误,但我认为这是一种非常糟糕的方法。

public class MainActivity extends Activity {
    public void onBackPressed()  {
        // You need to re-findView and re-setOnClickListener to button-button3
        setMain();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setMain();
    }

    public void setMain() {
        setContentView(R.layout.activity_main); 

        // This is for Audi A4
        Button button = (Button) findViewById(R.id.a4button);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                setContentView(R.layout.audi);
            }
        }); 

        // This is for Audi A6
        Button button1 = (Button) findViewById(R.id.a6button);
        button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                setContentView(R.layout.audia6);
            }
        });

        // This is for Audi Q5
        Button button2 = (Button) findViewById(R.id.q5button);
        button2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                setContentView(R.layout.audiq5);
            }
        }); 

        // This is for Audi R8
        Button button3 = (Button) findViewById(R.id.r8button);
        button3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                setContentView(R.layout.audir8);
            }
        });
    }
}

你可以创建很多活动,那该怎么办?

  1. 创建另一个扩展Activity

    的类
    public class ChildActivity extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            // Create another child.xml in your res > layout
            setContentView(R.layout.child);
        }
    }
    
  2. activity

    中的application标记下注册AndroidManifest.xml
    <activity name=".ChildActivity"/>
    
  3. ChildActivity

    致电MainActivity
    // This is for Audi A4
    Button button = (Button) findViewById(R.id.a4button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent childIntent = new Intent(MainActivity.this, ChildActivity.class);
            startActivity(childIntent); 
        }
    });
    
  4. 就是这样。我希望我没有遗漏任何东西。

    注意

    如果您要使用activity方法。您应该删除onBackPressed中的MainActivity方法。

答案 1 :(得分:0)

// try this way
**main.xml**
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:gravity="center"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/lnrButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <Button
                android:id="@+id/btnAudi4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Audi4"/>

            <Button
                android:id="@+id/btnAudi6"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Audi6"/>

            <Button
                android:id="@+id/btnAudiQ5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="AudiQ5"/>

            <Button
                android:id="@+id/btnAudiR8"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="AudiR8"/>

        </LinearLayout>

        <LinearLayout
            android:id="@+id/lnrAudi4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Audi4"/>

            <!--Write Audi4 layout shwon code here-->
        </LinearLayout>

        <LinearLayout
            android:id="@+id/lnrAudi6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Audi6"/>

            <!--Write Audi6 layout shwon code here-->
        </LinearLayout>

        <LinearLayout
            android:id="@+id/lnrAudiQ5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="AudiQ5"/>

            <!--Write AudiQ5 layout shwon code here-->
        </LinearLayout>

        <LinearLayout
            android:id="@+id/lnrAudiR8"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="AudiR8"/>

            <!--Write AudiR8 layout shwon code here-->
        </LinearLayout>

    </LinearLayout>
</ScrollView>

**MainActivity**
public class MyActivity extends Activity {

    private Button btnAudi4;
    private Button btnAudi6;
    private Button btnAudiQ5;
    private Button btnAudiR8;

    private LinearLayout lnrButton;
    private LinearLayout lnrAudi4;
    private LinearLayout lnrAudi6;
    private LinearLayout lnrAudiQ5;
    private LinearLayout lnrAudiR8;

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

        lnrButton = (LinearLayout) findViewById(R.id.lnrButton);
        lnrAudi4 = (LinearLayout) findViewById(R.id.lnrAudi4);
        lnrAudi6 = (LinearLayout) findViewById(R.id.lnrAudi6);
        lnrAudiQ5 = (LinearLayout) findViewById(R.id.lnrAudiQ5);
        lnrAudiR8 = (LinearLayout) findViewById(R.id.lnrAudiR8);

        btnAudi4 = (Button) findViewById(R.id.btnAudi4);
        btnAudi6 = (Button) findViewById(R.id.btnAudi6);
        btnAudiQ5 = (Button) findViewById(R.id.btnAudiQ5);
        btnAudiR8 = (Button) findViewById(R.id.btnAudiR8);

        btnAudi4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                lnrButton.setVisibility(View.GONE);
                lnrAudi4.setVisibility(View.VISIBLE);
            }
        });
        btnAudi6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                lnrButton.setVisibility(View.GONE);
                lnrAudi6.setVisibility(View.VISIBLE);
            }
        });
        btnAudiQ5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                lnrButton.setVisibility(View.GONE);
                lnrAudiQ5.setVisibility(View.VISIBLE);
            }
        });
        btnAudiR8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                lnrButton.setVisibility(View.GONE);
                lnrAudiR8.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public void onBackPressed() {
        if(lnrButton.getVisibility()==View.GONE){
            lnrButton.setVisibility(View.VISIBLE);
            lnrAudi4.setVisibility(View.GONE);
            lnrAudi6.setVisibility(View.GONE);
            lnrAudiQ5.setVisibility(View.GONE);
            lnrAudiR8.setVisibility(View.GONE);
        }else{
            super.onBackPressed();
        }
    }
}