目标就是在ListView上面实现这个效果
首先我们定义头部相对应的xml文件(使用三个button来实现)使用android:layout_weight属性使三个button等分头部
[html] view plaincopy
- <?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="40dp"
- android:orientation="horizontal" android:id="@+id/select_layout">
- <RelativeLayout android:layout_width="fill_parent" android:layout_height="40dp"
- android:layout_weight="1">
- <Button
- android:id="@+id/left_Btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="模拟题库"
- android:textSize="18.0sp"
- android:focusable="true"
- android:focusableInTouchMode="true"
- android:textColor="#FF3F3F3F"
- android:background="@drawable/header_button_selector"
- android:layout_centerInParent="true"/>
- </RelativeLayout>
- <RelativeLayout android:layout_width="fill_parent" android:layout_height="40dp"
- android:layout_weight="1">
- <Button
- android:id="@+id/center_Btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="真题库"
- android:textSize="18.0sp"
- android:focusable="true"
- android:focusableInTouchMode="true"
- android:textColor="#FF3F3F3F"
- android:background="@drawable/header_button_selector"
- android:layout_centerInParent="true"/>
- </RelativeLayout>
- <RelativeLayout android:layout_width="fill_parent" android:layout_height="40dp"
- android:layout_weight="1">
- <Button
- android:id="@+id/right_Btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="易错题库"
- android:focusable="true"
- android:focusableInTouchMode="true"
- android:textSize="18.0sp"
- android:textColor="#FF3F3F3F"
- android:background="@drawable/header_button_selector"
- android:layout_centerInParent="true"/>
- </RelativeLayout>
- </LinearLayout>
为了实现点击时候背景变色 需要定义header_button_selector 这个selector
[html] view plaincopy
- <?xml version="1.0" encoding="utf-8"?>
- <selector
- xmlns:android="http://schemas.android.com/apk/res/android"
- >
- <item
- android:state_window_focused="false"
- android:drawable="@color/white"
- >
- </item>
- <item
- android:state_focused="true"
- android:state_pressed="true"
- android:drawable="@color/head_btn_bac"
- >
- </item>
- <item
- android:state_focused="false"
- android:state_pressed="true"
- android:drawable="@color/head_btn_bac"
- >
- </item>
- <item
- android:state_selected="true"
- android:drawable="@color/head_btn_bac"
- >
- </item>
- <item
- android:state_focused="true"
- android:drawable="@color/head_btn_bac"
- >
- </item>
- </selector><span style="color:#ff0000;">
- </span>
我们定义一个类来创建头部的View。这样在Activity中就能复用
[java] view plaincopy
- package com.up591.android.view.componet;
- import com.up591.android.R;
- import com.up591.android.common.var.ColorEx;
- import com.up591.android.common.var.Constants;
- import com.up591.android.common.var.SharedPreferenceConstants;
- import com.up591.android.util.SharedPreferencesUtil;
- import com.up591.android.view.MainActivity;
- import com.up591.android.view.PractiseActivity;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Color;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class HeaderSelectView {
- private Activity activity;
- private Button leftBtn;
- private Button centerBtn;
- private Button rightBtn;
- public HeaderSelectView(Activity activity){
- this.activity = activity;
- }
- public View build(){
- View selectView = LayoutInflater.from(activity).inflate(R.layout.select_layout, null);
- leftBtn = (Button) selectView.findViewById(R.id.left_Btn);
- centerBtn = (Button) selectView.findViewById(R.id.center_Btn);
- rightBtn = (Button) selectView.findViewById(R.id.right_Btn);
- leftBtn.setText(Constants.bankTags[0]);
- centerBtn.setText(Constants.bankTags[1]);
- rightBtn.setText(Constants.bankTags[2]);
- int index = getIndex(activity);
- switch(index){
- case 1://选中中间
- setBtnFocusState(centerBtn);
- break;
- case 2:
- //选中右边
- setBtnFocusState(rightBtn);
- break;
- default:
- //选中左边 默认
- setBtnFocusState(leftBtn);
- break;
- }
- leftBtn.setOnClickListener(new OnClickListener(){
- public void onClick(View v) {
- setIndex(activity,0);
- forwardNewState();
- }
- });
- centerBtn.setOnClickListener(new OnClickListener(){
- public void onClick(View v) {
- setIndex(activity,1);
- forwardNewState();
- }
- });
- rightBtn.setOnClickListener(new OnClickListener(){
- public void onClick(View v) {
- setIndex(activity,2);
- forwardNewState();
- }
- });
- return selectView;
- }
- private void forwardNewState() {
- Intent intent = activity.getIntent();
- if(activity.getClass().equals(PractiseActivity.class)){
- intent.setClass(activity,MainActivity.class);
- }else{
- intent.setClass(activity,activity.getClass());
- }
- activity.startActivity(intent);
- }
- /**
- * 设置选中时候的状态
- * @param button
- */
- private void setBtnFocusState(Button button) {
- button.setTextColor(Color.WHITE);
- button.setBackgroundColor(ColorEx.HEAD_BTN_BAC);
- }
- /**
- * 获取得到当前选中的位置
- * @return
- */
- public static int getIndex(Context context) {
- SharedPreferencesUtil sp = SharedPreferencesUtil.getSyscfgSp(context);
- int index = sp.getIntValue(SharedPreferenceConstants.SYSCFG_QUESTIONS_TYPE_INDEX);
- return index;
- }
- /**
- * 设置选中的题库类型
- */
- public static void setIndex(Context context,int index){
- SharedPreferencesUtil sp = SharedPreferencesUtil.getSyscfgSp(context);
- sp.putIntValue(SharedPreferenceConstants.SYSCFG_QUESTIONS_TYPE_INDEX, index);
- }
- }<span style="color:#ff0000;">
- </span>
这里面有一点需要特别说清楚的:
很多网络上说button的setBackgroudColor不能设置进去
本人在开始操作的时候也发现这个问题。后来测试中发现用Color类的颜色是可以的
其实这里面需要的是一个16进制的数而不是color.xml 中定义的资源。所以我们可以定义一个int常量传进去
[java] view plaincopy
- package com.up591.android.common.var;
- public class ColorEx {
- /**头部按钮背景色--0xFFFEBE0C**/
- public static final int HEAD_BTN_BAC = 0xFFFEBE0C;
- }