同一个activity中通过一个按钮切换两个不同的布局 分类: And…

  • Post author:
  • Post category:其他


最近项目中需实现在同一个activity中通过一个按钮切换两个不同的布局,此处的按钮并非同一个按钮控件,

而是在两个不同的布局文件中点击按钮实现布局的切换,具体实现如下:

MenuTestActivity.java

package com.example.menutest;

import com.example.menutext.R;

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

public class MenuTestActivity extends Activity {
	public static int flag = 0;
	private LinearLayout.LayoutParams lp;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.double_frame);
		// Button but = getBtnBack();
		lp = new LinearLayout.LayoutParams(150,
				LinearLayout.LayoutParams.FILL_PARENT);
		lp.setMargins(-150, 0, 0, 0);
		((LinearLayout) findViewById(R.id.leftImage)).setLayoutParams(lp);

		initButton();
	}

	public void initButton() {

		Button button01 = (Button) findViewById(R.id.btnBack01);
		Button btnBack = (Button) findViewById(R.id.btnBack);

		button01.setText("Open");
		button01.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (flag == 0) {
					// 第一次单击触发的事件
					LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
							LinearLayout.LayoutParams.FILL_PARENT,
							LinearLayout.LayoutParams.FILL_PARENT);
					lp.setMargins(0, 0, 0, 0);
					((LinearLayout) findViewById(R.id.leftImage))
							.setLayoutParams(lp);
					System.out.println("1,第一次单击触发的事件");
					flag = 1;
				}
			}
		});
		btnBack.setText("Close");
		btnBack.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (flag == 1) {
					// 第二次单击button.text改变触发的事件
					((LinearLayout) findViewById(R.id.leftImage))
							.setLayoutParams(lp);
					System.out.println("2,第二次单击button.text改变触发的事件");
					flag = 0;
				}
			}
		});
	}

}

double_frame.layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/leftImage"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="-220dp"
        android:background="@drawable/btn_dial_normal" >

        <include
            android:id="@+id/include01"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            layout="@layout/base_layout_l" >
        </include>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/rightImage"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/btn_dial" >

        <include
            android:id="@+id/include02"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            layout="@layout/base_layout_r" >
        </include>
    </LinearLayout>

</LinearLayout>

base_layout_l.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearTop"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/icon" >
        </ImageView>

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:height="40dip"
            android:text="Dynamic 2" >
        </TextView>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearMain"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.0" >
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/relativeBottomTotal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/btnBack"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" 返  回 " >
        </Button>

        <LinearLayout
            android:id="@+id/layoutBottom"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/btnBack"
            android:layout_alignTop="@+id/btnBack"
            android:layout_toRightOf="@+id/btnBack"
            android:gravity="right" >
        </LinearLayout>
    </RelativeLayout>

</LinearLayout>

base_layout_r.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearTop"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/icon" >
        </ImageView>

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:height="40dip"
            android:text="Dynamic 1" >
        </TextView>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearMain"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.0" >
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/relativeBottomTotal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/btnBack01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" 1 " >
        </Button>

        <LinearLayout
            android:id="@+id/layoutBottom"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/btnBack"
            android:layout_alignTop="@+id/btnBack"
            android:layout_toRightOf="@+id/btnBack"
            android:gravity="right" >
        </LinearLayout>
    </RelativeLayout>

</LinearLayout>



源码下载地址:http://download.csdn.net/detail/u010963246/8970561

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/xieping/p/4714144.html