Android 自定义Tabhost
2015-07-10 15:00
447 查看
转载自:/article/3474695.html
首先上效果图
5张用到的图
andy.jpg
bill.jpg
torvalds.jpg
con_film_bottom_tab.9.png
con_film_tab_pressed.9.png
首先上效果图
1.先上AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.priscilla" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="7" /> <application android:icon="@drawable/icon" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".MyTab" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".TabContent" > </activity> </application> </manifest> 2 MyTab.java package com.priscilla; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.WeakHashMap; import android.app.TabActivity; import android.content.Context; import android.content.Intent; import android.content.res.Resources; import android.os.Bundle; //import android.view.LayoutInflater; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.LinearLayout; import android.widget.TabHost; import android.widget.TabWidget; import android.widget.TextView; import android.widget.TabHost.TabSpec; public class MyTab extends TabActivity { /** Called when the activity is first created. */ private TabHost mTabhost; private TabWidget mTabWidget; private LayoutInflater mInflater; private List<TextView> mtext; private List<TabSpec> mTabSpec; private List<LinearLayout> linearLayout; private List<Intent> intent; private Context mContext; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mContext = this; mInflater = LayoutInflater.from(this); // mTabhost = this.getTabHost();// 从TabActivity上面获取放置Tab的TabHost mTabhost = (TabHost) findViewById(android.R.id.tabhost); mTabWidget = (TabWidget) findViewById(android.R.id.tabs); mTabSpec = new ArrayList<TabSpec>(); linearLayout = new ArrayList<LinearLayout>(); mtext = new ArrayList<TextView>(); intent = new ArrayList<Intent>(); creatTab(); } public void creatTab() { for (int i = 0; i < 3; i++) { mTabSpec.add(mTabhost.newTabSpec("tab" + i)); linearLayout.add((LinearLayout) mInflater.inflate( R.layout.tabwidget, null)); mtext.add((TextView) linearLayout.get(i) .findViewById(R.id.tab_name)); mtext.get(i).setText( mContext.getResources().getString(R.string.tab_name, String.valueOf(i))); mTabSpec.get(i).setIndicator(linearLayout.get(i)); //mTabSpec.get(i).setContent(list.get(i)); intent.add(new Intent(mContext, TabContent.class)); Log.v("---whty---", mTabSpec.get(i).getTag()); Bundle buddle = new Bundle(); buddle.putString("tab", mTabSpec.get(i).getTag()); intent.get(i).putExtras(buddle); mTabSpec.get(i).setContent(intent.get(i)); mTabhost.addTab(mTabSpec.get(i)); } } } 2.TabContent.java package com.priscilla; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.widget.ImageView; import android.widget.TextView; public class TabContent extends Activity { private TextView textView; private ImageView imgView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.tabcontent); textView = (TextView) findViewById(R.id.TextView); imgView = (ImageView) findViewById(R.id.ImageView); Bundle buddle = this.getIntent().getExtras(); String flag = buddle.getString("tab"); if ("tab0".equals(flag)) { textView.setText(getResources().getString(R.string.andy)); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.andy); imgView.setImageBitmap(bitmap); } if ("tab1".equals(flag)) { textView.setText(getResources().getString(R.string.bill)); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bill); imgView.setImageBitmap(bitmap); } if ("tab2".equals(flag)) { textView.setText(getResources().getString(R.string.linux)); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.torvalds); imgView.setImageBitmap(bitmap); } } } 3.3个layout main.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="fill_parent" android:layout_height="fill_parent" android:background="@color/wcity_normal_bg" > <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" android:background="@drawable/con_film_bottom_tab" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" > </FrameLayout> </LinearLayout> </TabHost> tabwidget.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="horizontal" android:gravity="center" > <TextView android:id="@+id/tab_name" android:layout_width="fill_parent" android:layout_height="39dp" android:layout_marginLeft="2dp" android:layout_marginRight="2dp" android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" android:singleLine="true" android:gravity="center" android:textColor="@drawable/tab_selector" android:background="@drawable/tab_bg_selector" /> </LinearLayout> tabcontent.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_horizontal" android:orientation="vertical" > <ImageView android:id="@+id/ImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:scaleType="fitXY" /> <TextView android:id="@+id/TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="24dip" /> </LinearLayout> 4.2个drawable tab_bg_selector.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/con_film_tab_pressed" /> </selector> tab_selector.xml <?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="false" android:color="#ffffffff"/> <item android:state_selected="true" android:color="#ffEEC900"/> </selector>
5张用到的图
andy.jpg
bill.jpg
torvalds.jpg
con_film_bottom_tab.9.png
con_film_tab_pressed.9.png
6.colors.xml string.xml colors.xml <?xml version="1.0" encoding="utf-8"?> <resources> <color name="wcity_normal_bg">#ffe5eff4</color> </resources> string.xml <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, my</string> <string name="app_name">myTab</string> <string name="andy">Android的创造者: Andy Rubin</string> <string name="bill">Java创造者之一: Bill Joy</string> <string name="linux">Linux之父: Linus Torvalds</string> <string name="tab_name">选项卡%1$s</string> </resources>
相关文章推荐
- android的四种加载模式
- AndroidAnnotations(Code Diet)android快速开发框架
- android apk 代码混淆
- 启动android后没有应用图标,找不到应用程序
- Android Xutils 框架
- Android-Google自己的下拉刷新组件SwipeRefreshLayout
- AndroidManifest.xml文件解析
- Android开发之在子线程中使用Toast
- Android的ListView中判断其内容已滚动到最顶部或者最底部
- 禁止Android 横屏竖屏切换 .
- 解决Ubuntu adb无法连接上android手机
- android在一个activity中finish掉另外一个activity
- Android初学之------Log 工具类,方便查看打印log的位置 类名信息
- android定时执行任务
- 每位 Android 开发人员不容错过的十大实用工具
- Android中的Handler机制
- Android中的Handler机制 分类: Android 2015-07-10 14:15 9人阅读 评论(0) 收藏
- Android中moveTo、lineTo、quadTo、cubicTo、arcTo详解(实例)
- Android反编译(操作简单、方便可用)
- JS和Android相互调用