zoukankan      html  css  js  c++  java
  • android自定义TabWidget样式

    先看看效果图吧,个人觉得图标丑了点,不过还行,自己用PS做的


    下面是全部代码和流程,一定要按流程顺序来,不然错误!

    1.tabhost.xml

    1. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
    2.     xmlns:tools="http://schemas.android.com/tools"  
    3.     android:id="@android:id/tabhost"  
    4.     android:layout_width="fill_parent"  
    5.     android:layout_height="wrap_content" >  
    6.   
    7.     <RelativeLayout  
    8.         android:id="@+id/relativelayout"  
    9.         android:layout_width="fill_parent"  
    10.         android:layout_height="fill_parent" >  
    11.   
    12.         <FrameLayout  
    13.             android:id="@android:id/tabcontent"  
    14.             android:layout_width="fill_parent"  
    15.             android:layout_height="wrap_content" >  
    16.   
    17.             <LinearLayout  
    18.                 android:id="@+id/tab1"  
    19.                 android:layout_width="fill_parent"  
    20.                 android:layout_height="wrap_content"  
    21.                 android:orientation="vertical" >  
    22.             </LinearLayout>  
    23.   
    24.             <LinearLayout  
    25.                 android:id="@+id/tab2"  
    26.                 android:layout_width="fill_parent"  
    27.                 android:layout_height="wrap_content"  
    28.                 android:orientation="vertical" >  
    29.             </LinearLayout>  
    30.   
    31.             <LinearLayout  
    32.                 android:id="@+id/tab3"  
    33.                 android:layout_width="fill_parent"  
    34.                 android:layout_height="wrap_content"  
    35.                 android:orientation="vertical" >  
    36.             </LinearLayout>  
    37.   
    38.             <LinearLayout  
    39.                 android:id="@+id/tab4"  
    40.                 android:layout_width="fill_parent"  
    41.                 android:layout_height="wrap_content"  
    42.                 android:orientation="vertical" >  
    43.             </LinearLayout>  
    44.   
    45.             <LinearLayout  
    46.                 android:id="@+id/tab5"  
    47.                 android:layout_width="fill_parent"  
    48.                 android:layout_height="wrap_content"  
    49.                 android:orientation="vertical" >  
    50.             </LinearLayout>  
    51.         </FrameLayout>  
    52.   
    53.         <TabWidget  
    54.             android:id="@android:id/tabs"  
    55.             android:layout_width="fill_parent"  
    56.             android:layout_height="wrap_content"  
    57.             android:layout_alignParentBottom="true"  
    58.             android:background="@drawable/tabwidget_bj" >  
    59.         </TabWidget>  
    60.     </RelativeLayout>  
    61.   
    62. </TabHost>  

    2.tab_item_view.xml

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="wrap_content"  
    4.     android:layout_height="wrap_content"  
    5.     android:orientation="vertical" >  
    6.   
    7.     <ImageView  
    8.         android:id="@+id/imageview"  
    9.         android:layout_width="wrap_content"  
    10.         android:layout_height="wrap_content"  
    11.         android:layout_gravity="center_horizontal|top"  
    12.         android:padding="3dp" />  
    13.   
    14.     <TextView  
    15.         android:id="@+id/textview"  
    16.         android:layout_width="wrap_content"  
    17.         android:layout_height="wrap_content"  
    18.         android:layout_gravity="center_horizontal|bottom"  
    19.         android:textColor="#fff"  
    20.         android:textSize="13sp"   
    21.         style="bold"/>  
    22.   
    23. </LinearLayout>  

    3.样式选择器selector:tab_item_style.xml,新建文件夹drawable,然后将该xml文件放进去

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
    3.   
    4.     <item android:drawable="@drawable/unpressed_bj" android:state_selected="false"/>  
    5.     <item android:drawable="@drawable/pressed_bj" android:state_selected="true"/>  
    6.   
    7. </selector>  

    4.java代码实现:MyTabHost.java

      1. package com.example.androidtabhost4;  
      2.   
      3. import android.os.Bundle;  
      4. import android.app.Activity;  
      5. import android.app.TabActivity;  
      6. import android.content.Intent;  
      7. import android.view.LayoutInflater;  
      8. import android.view.Menu;  
      9. import android.view.View;  
      10. import android.widget.ImageView;  
      11. import android.widget.TabHost;  
      12. import android.widget.TextView;  
      13. import android.widget.TabHost.TabSpec;  
      14.   
      15. public class MyTabHost extends TabActivity {  
      16.   
      17.     private TabHost tabHost;  
      18.     private LayoutInflater layoutInflater;  
      19.     String[] mTitle = new String[] { "首页""留言""评论""收藏""更多" };  
      20.     int[] mIcon = new int[] { R.drawable.home, R.drawable.saying,  
      21.             R.drawable.zan, R.drawable.collect, R.drawable.more };  
      22.     int[] mTab = new int[] { R.id.tab1, R.id.tab2, R.id.tab3, R.id.tab4,  
      23.             R.id.tab5 };  
      24.   
      25.     @Override  
      26.     protected void onCreate(Bundle savedInstanceState) {  
      27.         super.onCreate(savedInstanceState);  
      28.         setContentView(R.layout.tabhost);  
      29.         init();  
      30.     }  
      31.   
      32.     public View getTabItemView(int i) {  
      33.         // TODO Auto-generated method stub  
      34.         View view = layoutInflater.inflate(R.layout.tab_item_view, null);  
      35.         ImageView imageView = (ImageView) view.findViewById(R.id.imageview);  
      36.         imageView.setImageResource(mIcon[i]);  
      37.         TextView textView = (TextView) view.findViewById(R.id.textview);  
      38.         textView.setText(mTitle[i]);  
      39.         return view;  
      40.     }  
      41.   
      42.     public void init() {  
      43.         // TODO Auto-generated method stub  
      44.         tabHost = getTabHost();  
      45.         layoutInflater = LayoutInflater.from(this);  
      46.         for (int i = 0; i < mTitle.length; i++) {  
      47.             TabSpec tabSpec = tabHost.newTabSpec(mTitle[i])  
      48.                     .setIndicator(getTabItemView(i)).setContent(mTab[i]);  
      49.             tabHost.addTab(tabSpec);  
      50.             tabHost.getTabWidget().getChildAt(i)  
      51.                     .setBackgroundResource(R.drawable.tab_item_style);  
      52.             tabHost.setup();  
      53.         }  
      54.     }  
      55.   

  • 相关阅读:
    Java8新特性
    搜索解决方案 -- ElasticSearch入门
    插入排序
    单点登录系统CAS入门
    快速排序
    选择性排序
    冒泡排序
    springcloud入门
    消息中间件 -- RabbitMQ
    ActiveMQ中消息的重发与持久化保存
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/3479830.html
Copyright © 2011-2022 走看看