自动生成TabHost,TabHost背景颜色设置

来源:互联网 发布:日本文学 知乎 编辑:程序博客网 时间:2024/06/05 02:05

1.布局、配置文件

AndroidManifest.xml

[html] view plaincopy
  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  2.       package="com.dsp.tvshow"  
  3.       android:versionCode="1"  
  4.       android:versionName="1.0">  
  5.     <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />  
  6.     <supports-screens  
  7.         android:smallScreens="true"  
  8.         android:normalScreens="true"  
  9.         android:largeScreens="true"  
  10.         android:xlargeScreens="true" />  
  11.       
  12.     <uses-permission android:name="android.permission.INTERNET" />  
  13.   
  14.     <application   
  15.         android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"  
  16.         android:icon="@drawable/icon" android:label="@string/app_name"  
  17.         android:logo="@drawable/logo">  
  18.         <activity android:name="com.dsp.activity.MainActivity" android:label="@string/app_name">  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.                 <category android:name="android.intent.category.LAUNCHER" />  
  22.             </intent-filter>  
  23.         </activity>  
  24.     </application>  
  25. </manifest>  

res/layout/layout_main.xml

[html] view plaincopy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <LinearLayout  
  8.         android:id="@+id/logoAreaLayout"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="50dp"   
  11.         android:gravity="left">  
  12.         <ImageView  
  13.             android:id="@+id/logImageView"  
  14.             android:layout_width="wrap_content"  
  15.             android:layout_height="fill_parent"  
  16.             android:src="@drawable/sitv_logo"   
  17.             android:paddingRight="30dp"  
  18.             android:layout_gravity="center"/>  
  19.   
  20.         <TextView  
  21.             android:id="@+id/logoTextView"  
  22.             android:layout_width="wrap_content"  
  23.             android:layout_height="fill_parent"  
  24.             android:text="@string/logo_text"   
  25.             android:textSize="30dp"  
  26.             android:gravity="center"/>  
  27.     </LinearLayout>  
  28.   
  29.     <TabHost   
  30.         android:id="@android:id/tabhost"  
  31.         android:layout_width="fill_parent"  
  32.         android:layout_height="wrap_content" >  
  33.         <LinearLayout android:id="@+id/tabLinearLayout"   
  34.             android:layout_height="fill_parent"   
  35.             android:layout_width="fill_parent"   
  36.             android:orientation="vertical">       
  37.             <TabWidget android:id="@android:id/tabs"   
  38.                 android:layout_height="wrap_content"   
  39.                 android:layout_width="fill_parent"  
  40.                 android:layout_weight="0"  
  41.                 android:gravity="center">  
  42.             </TabWidget>  
  43.             <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent">  
  44.                 <RelativeLayout android:id="@+id/videoInfoArea"  
  45.                     android:layout_width="fill_parent"  
  46.                     android:layout_height="50dp" >  
  47.                     <TextView   
  48.                         android:id="@+id/label"   
  49.                         android:layout_width="wrap_content"   
  50.                         android:layout_height="fill_parent"   
  51.                         android:gravity="center"  
  52.                          android:layout_marginLeft="200dp"  
  53.                         android:layout_alignParentLeft="true"  
  54.                         android:text="一共有1000个视频"  
  55.                         android:textSize="30dp"/>   
  56.                     <TextView   
  57.                         android:id="@+id/label"   
  58.                         android:layout_width="wrap_content"   
  59.                         android:layout_height="fill_parent"   
  60.                         android:gravity="center"  
  61.                         android:layout_marginRight="400dp"  
  62.                         android:layout_alignParentRight="true"  
  63.                         android:layout_marginLeft="60dp"  
  64.                         android:text="1/100页"  
  65.                         android:textSize="30dp"/>   
  66.                     <Button   
  67.                         android:id="@+id/forwardBtn"   
  68.                         android:layout_width="wrap_content"   
  69.                         android:layout_height="wrap_content"   
  70.                         android:layout_toLeftOf="@id/label"  
  71.                         android:layout_marginLeft="30dp"  
  72.                         android:text="@string/forwardBtnText"   
  73.                         android:textSize="20dp"/>    
  74.                     <Button   
  75.                         android:id="@+id/backwardBtn"   
  76.                         android:layout_width="wrap_content"   
  77.                         android:layout_height="wrap_content"   
  78.                         android:layout_toLeftOf="@id/forwardBtn"   
  79.                         android:text="@string/backwardBtnText"   
  80.                         android:textSize="20dp"/>                                          
  81.                 </RelativeLayout>         
  82.                 <GridView xmlns:android="http://schemas.android.com/apk/res/android"  
  83.                     android:id="@+id/videoGridView"  
  84.                     android:layout_width="fill_parent"  
  85.                     android:layout_height="fill_parent"  
  86.                     android:columnWidth="90dp"  
  87.                     android:numColumns="5"  
  88.                     android:verticalSpacing="10dp"  
  89.                     android:horizontalSpacing="10dp"  
  90.                     android:stretchMode="columnWidth"  
  91.                     android:gravity="center"  
  92.                 />                     
  93.             </FrameLayout>     
  94.         </LinearLayout>  
  95.     </TabHost>  
  96.   
  97.   
  98.   
  99. </LinearLayout>  


res/values/string.xml:
[html] view plaincopy
  1. <resources>  
  2.     <string name="app_name">TVShow</string>  
  3.     <string name="logo_text">ACR创新应用示范-互联网视频聚合</string>  
  4.     <string name="backwardBtnText">上一页</string>  
  5.     <string name="forwardBtnText">下一页</string>  
  6. </resources>  

res/values/styles.xml:
[html] view plaincopy
  1. <resources xmlns:android="http://schemas.android.com/apk/res/android">    
  2.     <!--  
  3.         Base application theme for API 11+. This theme completely replaces  
  4.         AppBaseTheme from res/values/styles.xml on API 11+ devices.  
  5.     -->  
  6. <style name="AppTheme" parent="android:Theme.Holo.Light">  
  7.     <!-- API 11 theme customizations can go here. -->  
  8. </style>  
  9.   
  10. </resources>  


res/drawable/tab_bg.xml:(配置tab背景图片)
[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector  
  3.   xmlns:android="http://schemas.android.com/apk/res/android">  
  4.     <item android:state_selected="true"  
  5.           android:drawable="@android:drawable/screen_background_light" />  
  6.     <item android:state_active="true"  
  7.           android:drawable="@android:drawable/alert_dark_frame" />  
  8.     <item android:drawable="@android:drawable/screen_background_dark" />  
  9. </selector>  

2.Java代码

MainActivity.java

[java] view plaincopy
  1. import java.util.List;  
  2.   
  3. import com.dsp.entity.gson.ProgramInfo;  
  4. import com.dsp.tvshow.R;  
  5. import com.dsp.util.ProgramInfoHelper;  
  6.   
  7. import android.app.TabActivity;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10. import android.widget.TabHost;  
  11. import android.widget.TabHost.OnTabChangeListener;  
  12. import android.widget.TabWidget;  
  13. import android.widget.TextView;  
  14.   
  15. public class MainActivity extends TabActivity implements OnTabChangeListener{  
  16.       
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.   
  21.         setContentView(R.layout.layout_main);  
  22.   
  23.         //获取栏目列表  
  24.         List<ProgramInfo> programs = new ProgramInfoHelper().getProgramList();  
  25.         TabHost tabHost = getTabHost();  
  26.         TabWidget tabWidget = tabHost.getTabWidget();  
  27.         for(ProgramInfo p : programs){  
  28.             TabHost.TabSpec tabSpec;  
  29.             //实例化一个分页  
  30.             tabSpec = tabHost.newTabSpec(p.getProgramId());   
  31. //          View tab = createTabIndicatorView(tabWidget, p.getProgramName(), getResources().getDrawable(R.drawable.tab_bg));  
  32. //          tabSpec.setIndicator(tab);  
  33.             //设置分页的标题  
  34.             tabSpec.setIndicator(p.getProgramName());     
  35.             //设置分页的内容     
  36.             tabSpec.setContent(android.R.id.tabcontent);      
  37.             tabHost.addTab(tabSpec);  
  38.         }  
  39.           
  40.         for(int i = 0; i < tabWidget.getChildCount(); i++){  
  41.             //获取tabview项   
  42.             View view = tabWidget.getChildTabViewAt(i);  
  43.             //设置tab背景颜色,对应配置文件的tab_bg.xml,可变化的背景,选中时为白色,未选中为黑色  
  44.             view.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_bg));  
  45.             //获取textview控件  
  46.             TextView textView = (TextView)view.findViewById(android.R.id.title);  
  47.             textView.setGravity(BIND_AUTO_CREATE);  
  48.             //自动变换颜色,但是没实现  
  49. //          textView.setTextColor(getResources().getColor(R.color.tabtext_color));    
  50.             if(i == 0){  
  51.                 //默认的初始页为第一页,tab的文字颜色设为白色  
  52.                 textView.setTextColor(getResources().getColor(android.R.color.black));  
  53.             }else{  
  54.                 //未选中的页的tab的文字设为黑色  
  55.                 textView.setTextColor(getResources().getColor(android.R.color.white));  
  56.             }  
  57.             //设置tab的文字大小  
  58.             textView.setTextSize(30);  
  59.         }  
  60.           
  61.         tabHost.setCurrentTab(0);  
  62.         //tabchanged的监听  
  63.         tabHost.setOnTabChangedListener(this);  
  64.           
  65.     }  
  66.   
  67.     //改变tab时的处理  
  68.     @Override  
  69.     /*  
  70.      * @see android.widget.TabHost.OnTabChangeListener#onTabChanged(java.lang.String) 
  71.      * @param tabId 新建TabSpec时设置的id 
  72.      */  
  73.     public void onTabChanged(String tabId) {  
  74.         TabHost tabHost = getTabHost();  
  75.         int curTabID = getTabHost().getCurrentTab();  
  76.         TabWidget tabWidget = tabHost.getTabWidget();  
  77.         View view = tabWidget.getChildTabViewAt(curTabID);  
  78.         TextView textView = (TextView)view.findViewById(android.R.id.title);  
  79.         //将选中页tab的文字设为黑色  
  80.         textView.setTextColor(getResources().getColor(android.R.color.black));  
  81.         for(int i = 0; i < tabWidget.getChildCount(); i++){  
  82.             if(i != curTabID){  
  83.                 //未选中页tab的文字设为白色  
  84.                 ((TextView)tabWidget.getChildTabViewAt(i).findViewById(android.R.id.title)).setTextColor(getResources().getColor(android.R.color.white));  
  85.             }  
  86.         }  
  87.     }  
  88.   
  89.   
  90.     /* 
  91.      * 生成标准TAB标头的工具 
  92.      * http://www.eoeandroid.com/thread-51167-1-1.html 
  93.      * @param parent The parent ViewGroup to attach the new view to. 
  94.      * @param label The label to display in the tab indicator. If null, not label will be displayed. 
  95.      * @param icon The icon to display. If null, no icon will be displayed. 
  96.      * @return The tab indicator View. 
  97.      */  
  98.     /* 
  99.     private View createTabIndicatorView(ViewGroup parent, CharSequence label, Drawable icon) { 
  100.         final LayoutInflater inflater = LayoutInflater.from(this); 
  101.         final View tabIndicator = inflater.inflate(R.layout.tab_indicator, parent, false); 
  102.  
  103.         final TextView tv = (TextView) tabIndicator.findViewById(R.id.tab_title); 
  104.         tv.setText(label); 
  105.  
  106.         final ImageView iconView = (ImageView) tabIndicator.findViewById(R.id.tab_icon); 
  107.         iconView.setImageDrawable(icon); 
  108.  
  109.         return tabIndicator; 
  110.     } 
  111.     */  
  112.   
  113. }  

3.效果



参考:

TabSpec和TabHost实例

android TabHost解决下面白线

如何设置TabHost标签里面的文字的颜色

[Android实例] 史上最全的Android的Tab与TabHost讲解

0 0