Android中TabHost的使用
16lz
2021-01-23
TabHostActivity.java代码:
package com.jswjtu.imageclassification.activity;import com.jswjtu.imageclassification.R;import android.app.TabActivity;import android.content.Intent;import android.content.res.Resources;import android.os.Bundle;import android.util.DisplayMetrics;import android.view.GestureDetector;import android.view.GestureDetector.SimpleOnGestureListener;import android.view.LayoutInflater;import android.view.MotionEvent;import android.view.Window;import android.widget.RelativeLayout;import android.widget.TabHost;import android.widget.TextView;@SuppressWarnings("deprecation")public class TabHostActivity extends TabActivity { Intent intent; TabHost.TabSpec tabSpec; TabHost tabHost; int currentTabID = 0; int flaggingWidth;// 互动翻页所需滚动的长度是当前屏幕宽度的1/3 GestureDetector gestureDetector = new GestureDetector(new TabHostTouch()); protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); //设置无标题 requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.tabhost); Resources res = getResources(); tabHost = getTabHost(); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); flaggingWidth = dm.widthPixels / 3; //给Tab1添加自定义样式 RelativeLayout tabStyle1 = (RelativeLayout)LayoutInflater.from(this).inflate(R.layout.tab_style, null); TextView text1 = (TextView)tabStyle1.findViewById(R.id.tab_label); text1.setText("相册"); //给Tab2添加自定义样式 RelativeLayout tabStyle2 = (RelativeLayout)LayoutInflater.from(this).inflate(R.layout.tab_style, null); TextView text2 = (TextView)tabStyle2.findViewById(R.id.tab_label); text2.setText("视频"); intent = new Intent(TabHostActivity.this,ImageActivity.class);//指定tab页面的Activity tabSpec = tabHost.newTabSpec("image"); // 创建一个新的标签页,标记为“image” tabSpec.setIndicator(tabStyle1);// 设置tab页的名称和图像表示 tabSpec.setContent(intent);// 设置此tab跳转到的Activity tabHost.addTab(tabSpec);// 将此tab加入到tabHost intent = new Intent(TabHostActivity.this,VideoActivity.class); tabSpec = tabHost.newTabSpec("video"); tabSpec.setIndicator(tabStyle2); tabSpec.setContent(intent); tabHost.addTab(tabSpec); tabHost.setCurrentTab(0);// 设置当期的tab页,从0开始偏移 } @Override public boolean dispatchTouchEvent(MotionEvent event) { if (gestureDetector.onTouchEvent(event)) { event.setAction(MotionEvent.ACTION_CANCEL); } return super.dispatchTouchEvent(event); } private class TabHostTouch extends SimpleOnGestureListener { @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if (Math.abs(e1.getX() - e2.getX()) > Math.abs(e1.getY() - e2.getY()) && (e1.getX() - e2.getX() <= (-flaggingWidth) || e1.getX() - e2.getX() >= flaggingWidth)) { currentTabID = tabHost.getCurrentTab(); if (e1.getX() - e2.getX() <= (-flaggingWidth)) { if (tabHost.getCurrentTab() == 1) { currentTabID = 0; } tabHost.setCurrentTab(currentTabID); return true; } else if (e1.getX() - e2.getX() >= flaggingWidth) { if (tabHost.getCurrentTab() == 0) { currentTabID = 1; } tabHost.setCurrentTab(currentTabID); return true; } } return false; } } }
tabhost.xml代码:
<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- 注意这里,id的设置方法跟普通控件不同,必须为tabhost --> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout></TabHost>
tab_style.xml代码:
<?xml version="1.0" encoding="UTF-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="5dip" android:paddingRight="5dip" android:background="#67e9bb" > <TextView android:id="@+id/tab_label" android:layout_width="fill_parent" android:layout_height="40dp" android:gravity="center" android:textColor="#000000" android:textStyle="bold" android:background="@drawable/tab" /> </RelativeLayout>
tab.xml代码:
<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android = "http://schemas.android.com/apk/res/android"> <item android:state_selected = "true" android:drawable="@drawable/tab_sel_ture" /> <item android:state_selected = "false" android:drawable="@drawable/tab_sel_false" /> </selector>
即可;
更多相关文章
- android,java代码设置背景色
- 移动应用开发:如何创建自定义Android代码模板
- Android 用代码动态添加View(ViewGroup.addView()),并进行动态添加
- Android应用程序进程启动过程的源代码分析(1)
- Android API之Typeface代码演示
- Android studio制作计算器源代码