Android中使用View实现选项卡功能

来源:互联网 发布:淘宝有好货报名条件 编辑:程序博客网 时间:2024/05/29 02:22

效果图


MainActivity中:
package com.example.testview;import android.app.Activity;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.LinearLayout;import android.widget.RelativeLayout;import android.widget.TextView;public class MainActivity extends Activity {    private TextView activity_change_1;    private TextView activity_change_2;    private TextView activity_change_3;    private LinearLayout llContentview;    View tab1View;    View tab2View;    View tab3View;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();        myOnclick();    }    private void initView() {        activity_change_1 = (TextView)findViewById(R.id.activity_change_1);        activity_change_2 = (TextView)findViewById(R.id.activity_change_2);        activity_change_3 = (TextView)findViewById(R.id.activity_change_3);        llContentview = (LinearLayout)findViewById(R.id.llContentview);        Tab1 tab1 =new Tab1(this);        tab1View=tab1.getView();        Tab2 tab2 = new Tab2(this);        tab2View=tab2.getView();        Tab3 tab3 = new Tab3(this);        tab3View=tab3.getView();        addTab1();    }    private void myOnclick() {        activity_change_1.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                addTab1();            }        });        activity_change_2.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                addTab2();            }        });        activity_change_3.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                addTab3();            }        });    }    private void  addTab1(){        if(tab1View!=null) {            llContentview.removeAllViews();            llContentview.addView(tab1View);//                  由于addView导致match_parent失效,这里设置的是tab1中最大的布局,LinearLayout.LayoutParams指的是activity_main中的llContentview//                  如果是相对布局则RelativeLayout.LayoutParams            tab1View.setLayoutParams(new android.widget.LinearLayout.LayoutParams(                    android.widget.LinearLayout.LayoutParams.MATCH_PARENT,                    android.widget.LinearLayout.LayoutParams.MATCH_PARENT));        }    }    private void  addTab2(){        if(tab1View!=null) {            llContentview.removeAllViews();            llContentview.addView(tab2View);            tab2View.setLayoutParams(new android.widget.LinearLayout.LayoutParams(                    android.widget.LinearLayout.LayoutParams.MATCH_PARENT,                    android.widget.LinearLayout.LayoutParams.MATCH_PARENT));        }    }    private void  addTab3(){        if(tab1View!=null) {            llContentview.removeAllViews();            llContentview.addView(tab3View);            tab3View.setLayoutParams(new android.widget.LinearLayout.LayoutParams(                    android.widget.LinearLayout.LayoutParams.MATCH_PARENT,                    android.widget.LinearLayout.LayoutParams.MATCH_PARENT));        }    }}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <LinearLayout        android:layout_width="match_parent"        android:orientation="horizontal"        android:layout_height="50dp"        android:background="#FFFFCC"        >        <TextView            android:id="@+id/activity_change_1"            android:layout_width="0dp"            android:layout_height="match_parent"            android:text="第1页"            android:layout_weight="1"            android:gravity="center"            android:textColor="#000"            />        <TextView            android:id="@+id/activity_change_2"            android:layout_width="0dp"            android:layout_height="match_parent"            android:text="第2页"            android:layout_weight="1"            android:gravity="center"            android:textColor="#000"            />        <TextView            android:id="@+id/activity_change_3"            android:layout_width="0dp"            android:layout_height="match_parent"            android:text="第3页"            android:layout_weight="1"            android:gravity="center"            android:textColor="#000"            />    </LinearLayout>    <!--里面放view的布局-->    <LinearLayout        android:id="@+id/llContentview"        android:orientation="vertical"        android:layout_height="match_parent"        android:layout_width="match_parent"        android:background="#FFFF66"        >    </LinearLayout></LinearLayout>
Tab1(Tab1,Tab2,Tab3都一样,这里只写Tab1
package com.example.testview;import android.content.Context;import android.view.View;import android.widget.Button;import android.widget.Toast;/** * Created by 16838 on 2017/8/10. */public class Tab1 {    private Context context;    View view=null;    public Tab1(Context context){        this.context=context;    }    public View getView(){        view= View.inflate(context,R.layout.tab1,null);        initView(view);        return view;    };    private void initView(View view){    }}
tab1.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:background="#FFCC66"    >    <TextView        android:id="@+id/btnButton"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="tab1"        /></LinearLayout>
源码下载:
MyWebView ---- testview
http://download.csdn.net/download/zhaihaohao1/9927529






原创粉丝点击