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
阅读全文
0 0
- Android中使用View实现选项卡功能
- Android中选项卡功能的实现
- Android中FragmentTabHost结合fragment实现选项卡功能
- Android 选项卡功能的实现
- 使用TabLayout 实现顶部选项卡功能
- 使用Android Support Design 控件TabLayout 方便快捷实现选项卡功能
- android 自定义view使用Canvas实现支付宝咻一咻功能
- Android使用TabLayout实现选项卡
- Fragments实现选项卡功能
- Jquery实现选项卡功能
- js实现选项卡功能
- Jquery实现选项卡功能
- js_day02_js实现选项卡功能
- jquery实现选项卡功能
- Javascript实现选项卡功能
- angularJS-实现选项卡功能
- js选项卡功能实现
- android中使用tab选项卡
- MVVM Light Toolkit使用指南
- ORACLE
- Frobenius norm(弗罗贝尼乌斯范数)
- 使用CSDN—MarkDown编辑数学公式
- android NetWorkInfo类的isConnected()与isAvailable()两个函数
- Android中使用View实现选项卡功能
- 查看liunx版本的命令
- 《去哪网编程题》filename extension
- 多核的idleTask的设定及其功能
- AJAX的代码
- HDU-2045 不容易系列之(3)—— LELE的RPG难题(递推)
- 170
- PHP实现用户异地登录提醒
- javascript几种写空格符的方法