好久没更新博客了,今天给大家分享一下Android中的资源与国际化的问题,通常我们新建一个Android工程,目录结构如下图所示: 我们主要看一下layout与values目录,layou

来源:互联网 发布:波士顿矩阵图案例 编辑:程序博客网 时间:2024/05/17 20:00

紧接上一篇巧妙运用ViewStub写出类似Tab选项卡(想怎么写tab就怎么写,横着写竖着写随你) ,上一篇耦合度实在是太高了(其实那个性能也不咋滴),饱受那一堆乱稻草捆绑在一起的痛苦,所以进行了一系列的改造。

第一步:首先你得了解TabHost和TabWidget, 将TabHost分为两个部分,一个是放内容的,还有一个就是放选项卡的,我们这里选项卡用TabWidget。TabHost的id应该写为

android:id="@android:id/tabhost",而不是以前的那个加号,这样可以直接在Activity里面通过getTabHost方法得到此TabHost对象。那如何才能将选项卡放在最底部呢,这其实很简单,还记得 RelativeLayout中android:layout_alignBottom 这个不,也就是说将TabWidget和选项卡内容放到RelativeLayout布局里,废话不多说了,看代码

view plain
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TabHost android:layout_width="fill_parent"  
  3.     android:layout_height="fill_parent"  
  4.     android:id="@android:id/tabhost"  
  5.     xmlns:android="http://schemas.android.com/apk/res/android"  
  6.     >  
  7.     <RelativeLayout  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent"  
  10.         android:orientation="vertical"  
  11.         android:padding="3dp"  
  12.     >  
  13.          <FrameLayout  
  14.              android:layout_width="fill_parent"  
  15.             android:layout_height="fill_parent"  
  16.             android:id="@android:id/tabcontent"  
  17.             android:layout_weight="1"   
  18.              >  
  19.          </FrameLayout>  
  20.           <TabWidget  
  21.             android:layout_width="fill_parent"  
  22.             android:layout_height="50dip"  
  23.             android:id="@android:id/tabs"  
  24.             android:layout_alignBottom = "@android:id/tabcontent"  
  25.             />  
  26.     </RelativeLayout>  
  27. </TabHost>  


以上xml代码可以通用哈,FrameLayout里用来放内容的,Tab的xml基本结构就这样搭好了,

第二步:接下来就是往Activity里面写点东东了,这个Activity必须要继承下TabActivity,要不然就算你setContentView了上面那个xml也没法通过getTabHost()获得布局中的tabHost。

然后通过getTabHost得到TabHost对象。我这里打算建立四个选项卡,newTabSpec 这是为你的每一个选项卡上面打上一个Tag,也就是标记标记而已,特殊情况下你可以通过这个来查找到你的选项卡。setIndicator就是在你的选项卡上设置一些内容,至于什么内容,你懂的,要么是文字,要么是图片,要么就是文字加图片,当然有时候复杂的话还会写上布局文件。我这里就放上一个图片吧,然后这个图片加上一些特效,懒得在xml写什么布局了,直接在代码写了,返回的是一个View对象,废话有点多,直接看代码吧

view plain
  1.   private  class TabView extends LinearLayout {  
  2.     ImageView imageView ;  
  3. public TabView(Context c, int drawable, int drawableselec) {  
  4.     super(c);  
  5.     imageView = new ImageView(c);  
  6.     StateListDrawable listDrawable = new StateListDrawable();  
  7.     listDrawable.addState(SELECTED_STATE_SET, this.getResources()  
  8.             .getDrawable(drawableselec));  
  9.     listDrawable.addState(ENABLED_STATE_SET, this.getResources()  
  10.             .getDrawable(drawable));  
  11.     imageView.setImageDrawable(listDrawable);  
  12.     imageView.setBackgroundColor(Color.TRANSPARENT);  
  13.     setGravity(Gravity.CENTER);  
  14.     addView(imageView);  
  15. }  


上面的代码也就是自定义的一个View吧。

下面这个就是构建选项卡以及内容(选中的是第二个选项卡),我这里就把每个不同的选项卡内容分别放在不同的Activity里面,然后通过Tab来将这些Activity合并在一起,这样显得比较有层次感。

view plain
  1. @Override  
  2.     public void onCreate(Bundle savedInstanceState) {  
  3.         super.onCreate(savedInstanceState);  
  4.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  5.         setContentView(R.layout.main_tab);  
  6.         TabHost tabHost=getTabHost();  
  7.           
  8.         TabView view = null;  
  9.           
  10.         // 最近联系人  
  11.         view = new TabView(this, R.drawable.bg_tab_dial_normal, R.drawable.bg_tab_dial_normal);  
  12.         view.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.selecttabbackground));  
  13.               
  14.         TabSpec recentContactSpec=tabHost.newTabSpec("RecentContact");  
  15.         recentContactSpec.setIndicator(view);  
  16.         Intent recentContactIntent = new Intent(this, RecentContactActivity.class);  
  17.         recentContactSpec.setContent(recentContactIntent);  
  18.         // 联系人  
  19.         view = new TabView(this, R.drawable.bg_tab_contact_normal, R.drawable.bg_tab_contact_normal);  
  20.         view.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.selecttabbackground));  
  21.           
  22.         TabSpec contactBookSpec=tabHost.newTabSpec("ContactBook");  
  23.         contactBookSpec.setIndicator(view);  
  24.         Intent contactBookIntent = new Intent(this,ContactBookActivity.class);  
  25.         contactBookSpec.setContent(contactBookIntent);  
  26.           
  27.         // 短信  
  28.         view = new TabView(this, R.drawable.bg_tab_sms_normal, R.drawable.bg_tab_sms_normal);  
  29.         view.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.selecttabbackground));  
  30.           
  31.         TabSpec smsMessageSpec = tabHost.newTabSpec("SmsMessage");  
  32.         smsMessageSpec.setIndicator(view);  
  33.         Intent smsMessageIntent = new Intent(this, SmsMessageActivity.class);  
  34.         smsMessageSpec.setContent(smsMessageIntent);  
  35.           
  36.         //设置   
  37.         view = new TabView(this, R.drawable.bg_tab_setting_normal, R.drawable.bg_tab_setting_normal);  
  38.         view.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.selecttabbackground));  
  39.           
  40.         TabSpec settingSpec = tabHost.newTabSpec("Setting");  
  41.         settingSpec.setIndicator(view);  
  42.         Intent settingIntent = new Intent(this, SettingActivity.class);  
  43.         settingSpec.setContent(settingIntent);  
  44.           
  45.         tabHost.addTab(recentContactSpec);  
  46.         tabHost.addTab(contactBookSpec);  
  47.         tabHost.addTab(smsMessageSpec);  
  48.         tabHost.addTab(settingSpec);  
  49.           
  50.         tabHost.setCurrentTab(1);  
  51.     }  
  52.       


这个我没有写Demo,也就不提供详细的Demo了,不过还是截个图吧