android listView头部自定义标签形式

目标就是在ListView上面实现这个效果
首先我们定义头部相对应的xml文件(使用三个button来实现)使用android:layout_weight属性使三个button等分头部

 

[html] view plaincopy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent" android:layout_height="40dp"  
  4.     android:orientation="horizontal" android:id="@+id/select_layout">  
  5.         <RelativeLayout android:layout_width="fill_parent" android:layout_height="40dp"  
  6.             android:layout_weight="1">  
  7.             <Button   
  8.                 android:id="@+id/left_Btn"  
  9.                 android:layout_width="wrap_content"  
  10.                 android:layout_height="wrap_content"   
  11.                 android:text="模拟题库"  
  12.                 android:textSize="18.0sp"   
  13.                 android:focusable="true"  
  14.                 android:focusableInTouchMode="true"  
  15.                 android:textColor="#FF3F3F3F"  
  16.                 android:background="@drawable/header_button_selector"  
  17.                 android:layout_centerInParent="true"/>    
  18.         </RelativeLayout>  
  19.         <RelativeLayout android:layout_width="fill_parent" android:layout_height="40dp"  
  20.             android:layout_weight="1">  
  21.             <Button   
  22.                 android:id="@+id/center_Btn"  
  23.                 android:layout_width="wrap_content"  
  24.                 android:layout_height="wrap_content"   
  25.                 android:text="真题库"  
  26.                 android:textSize="18.0sp"   
  27.                 android:focusable="true"  
  28.                 android:focusableInTouchMode="true"  
  29.                 android:textColor="#FF3F3F3F"  
  30.                 android:background="@drawable/header_button_selector"  
  31.                 android:layout_centerInParent="true"/>  
  32.         </RelativeLayout>  
  33.         <RelativeLayout android:layout_width="fill_parent" android:layout_height="40dp"  
  34.             android:layout_weight="1">  
  35.                 <Button   
  36.                 android:id="@+id/right_Btn"  
  37.                 android:layout_width="wrap_content"  
  38.                 android:layout_height="wrap_content"   
  39.                 android:text="易错题库"  
  40.                 android:focusable="true"  
  41.                 android:focusableInTouchMode="true"  
  42.                 android:textSize="18.0sp"   
  43.                 android:textColor="#FF3F3F3F"  
  44.                 android:background="@drawable/header_button_selector"  
  45.                 android:layout_centerInParent="true"/>  
  46.         </RelativeLayout>  
  47. </LinearLayout>  

 

为了实现点击时候背景变色 需要定义header_button_selector   这个selector

 

 

 

[html] view plaincopy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     >  
  5.     <item  
  6.         android:state_window_focused="false"  
  7.         android:drawable="@color/white"  
  8.         >  
  9.     </item>  
  10.     <item  
  11.         android:state_focused="true"  
  12.         android:state_pressed="true"  
  13.         android:drawable="@color/head_btn_bac"  
  14.         >  
  15.     </item>  
  16.     <item  
  17.         android:state_focused="false"  
  18.         android:state_pressed="true"  
  19.         android:drawable="@color/head_btn_bac"  
  20.         >  
  21.     </item>  
  22.     <item  
  23.         android:state_selected="true"  
  24.         android:drawable="@color/head_btn_bac"  
  25.         >  
  26.     </item>  
  27.     <item  
  28.         android:state_focused="true"  
  29.         android:drawable="@color/head_btn_bac"  
  30.         >  
  31.     </item>  
  32.       
  33. </selector><span style="color:#ff0000;">  
  34. </span>  

我们定义一个类来创建头部的View。这样在Activity中就能复用

 

 

[java] view plaincopy

  1. package com.up591.android.view.componet;  
  2.   
  3. import com.up591.android.R;  
  4. import com.up591.android.common.var.ColorEx;  
  5. import com.up591.android.common.var.Constants;  
  6. import com.up591.android.common.var.SharedPreferenceConstants;  
  7. import com.up591.android.util.SharedPreferencesUtil;  
  8. import com.up591.android.view.MainActivity;  
  9. import com.up591.android.view.PractiseActivity;  
  10.   
  11. import android.app.Activity;  
  12. import android.content.Context;  
  13. import android.content.Intent;  
  14. import android.graphics.Color;  
  15. import android.view.LayoutInflater;  
  16. import android.view.View;  
  17. import android.view.View.OnClickListener;  
  18. import android.widget.Button;  
  19.   
  20. public class HeaderSelectView {  
  21.       
  22.     private Activity activity;  
  23.       
  24.     private Button leftBtn;  
  25.       
  26.     private Button centerBtn;  
  27.       
  28.     private Button rightBtn;  
  29.       
  30.     public HeaderSelectView(Activity activity){  
  31.         this.activity = activity;  
  32.     }  
  33.       
  34.     public View build(){    
  35.         View selectView = LayoutInflater.from(activity).inflate(R.layout.select_layout, null);  
  36.         leftBtn = (Button) selectView.findViewById(R.id.left_Btn);    
  37.         centerBtn = (Button) selectView.findViewById(R.id.center_Btn);  
  38.         rightBtn = (Button) selectView.findViewById(R.id.right_Btn);  
  39.           
  40.         leftBtn.setText(Constants.bankTags[0]);  
  41.         centerBtn.setText(Constants.bankTags[1]);  
  42.         rightBtn.setText(Constants.bankTags[2]);  
  43.           
  44.           
  45.         int index = getIndex(activity);  
  46.         switch(index){  
  47.         case 1://选中中间  
  48.             setBtnFocusState(centerBtn);  
  49.             break;  
  50.         case 2:  
  51.             //选中右边  
  52.             setBtnFocusState(rightBtn);  
  53.             break;  
  54.         default:  
  55.           //选中左边  默认  
  56.             setBtnFocusState(leftBtn);  
  57.             break;  
  58.         }  
  59.           
  60.         leftBtn.setOnClickListener(new OnClickListener(){  
  61.   
  62.             public void onClick(View v) {  
  63.                 setIndex(activity,0);  
  64.                 forwardNewState();  
  65.             }  
  66.         });  
  67.           
  68.         centerBtn.setOnClickListener(new OnClickListener(){  
  69.   
  70.             public void onClick(View v) {  
  71.                 setIndex(activity,1);  
  72.                 forwardNewState();  
  73.             }  
  74.         });  
  75.           
  76.         rightBtn.setOnClickListener(new OnClickListener(){  
  77.   
  78.             public void onClick(View v) {  
  79.                 setIndex(activity,2);  
  80.                 forwardNewState();  
  81.             }  
  82.               
  83.         });  
  84.           
  85.         return selectView;  
  86.     }  
  87.     private void forwardNewState() {  
  88.         Intent intent = activity.getIntent();  
  89.         if(activity.getClass().equals(PractiseActivity.class)){  
  90.             intent.setClass(activity,MainActivity.class);  
  91.         }else{  
  92.             intent.setClass(activity,activity.getClass());  
  93.         }  
  94.         activity.startActivity(intent);  
  95.     }  
  96.       
  97.     /** 
  98.      * 设置选中时候的状态 
  99.      * @param button 
  100.      */  
  101.     private void setBtnFocusState(Button button) {    
  102.         button.setTextColor(Color.WHITE);  
  103.         button.setBackgroundColor(ColorEx.HEAD_BTN_BAC);  
  104.     }  
  105.   
  106.     /** 
  107.      * 获取得到当前选中的位置 
  108.      * @return 
  109.      */  
  110.     public static int getIndex(Context context) {  
  111.           
  112.         SharedPreferencesUtil sp = SharedPreferencesUtil.getSyscfgSp(context);  
  113.         int index = sp.getIntValue(SharedPreferenceConstants.SYSCFG_QUESTIONS_TYPE_INDEX);  
  114.         return index;  
  115.     }  
  116.     /** 
  117.      * 设置选中的题库类型 
  118.      */  
  119.     public static void setIndex(Context context,int index){  
  120.         SharedPreferencesUtil sp = SharedPreferencesUtil.getSyscfgSp(context);  
  121.         sp.putIntValue(SharedPreferenceConstants.SYSCFG_QUESTIONS_TYPE_INDEX, index);  
  122.     }  
  123. }<span style="color:#ff0000;">  
  124. </span>  

这里面有一点需要特别说清楚的:
很多网络上说button的setBackgroudColor不能设置进去
本人在开始操作的时候也发现这个问题。后来测试中发现用Color类的颜色是可以的
其实这里面需要的是一个16进制的数而不是color.xml 中定义的资源。所以我们可以定义一个int常量传进去

 

 

[java] view plaincopy

  1. package com.up591.android.common.var;  
  2.   
  3. public class ColorEx {  
  4.     /**头部按钮背景色--0xFFFEBE0C**/  
  5.     public static final int HEAD_BTN_BAC = 0xFFFEBE0C;  
  6. }  
  7.  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值