1. 程式人生 > >Android之切換佈局

Android之切換佈局

佈局1:

?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/black"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
        android:layout_width="186px"
        android:layout_height="29px"
        android:id="@+id/text1"
        android:layout_x="70px"
        android:layout_y="32px"
        android:text="黑絲!!!" />
    <Button
        android:layout_width="118px"
        android:layout_height="wrap_content"
        android:layout_x="100px"
        android:layout_y="82px"
        android:id="@+id/button1"
        android:text="去看白絲妹子"/>
</AbsoluteLayout>

佈局2

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/white">
    <TextView
        android:layout_width="186px"
        android:layout_height="29px"
        android:id="@+id/text2"
        android:layout_x="70px"
        android:layout_y="32px"
        android:text="白絲!!!"/>
    <Button
        android:layout_width="118px"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        android:layout_x="100px"
        android:layout_y="82px"
        android:text="去看黑絲妹子"/>
</AbsoluteLayout>

java程式碼

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button b1=(Button)findViewById(R.id.button1);
        b1.setOnClickListener(new Button.OnClickListener()
        {
            public void onClick(View v)
            {
                jumpToLayout2();
            }
        });
    }
   public void jumpToLayout2()
    {
        setContentView(R.layout.mylayout);
        Button b2=(Button)findViewById(R.id.button2);
        b2.setOnClickListener(new Button.OnClickListener()
        {
            public void onClick(View v)
            {
                jumpToLayout1();
            }
        });
    }
    public void jumpToLayout1()
    {
        setContentView(R.layout.activity_main);
        Button b1=(Button)findViewById(R.id.button1);
        b1.setOnClickListener(new Button.OnClickListener()
        {
            public void onClick(View v)
            {
                jumpToLayout2();
            }
        });
    }
}