你的位置:首页 > 操作系统

[操作系统]联系人的侧边字母索引ListView 将手机通讯录姓名通过首字母排序。


 

 

 1 package com.lixu.letterlistview; 2  3 import java.util.ArrayList; 4 import java.util.List; 5 import org.apache.http.NameValuePair; 6 import org.apache.http.message.BasicNameValuePair; 7 import com.lixu.letterlistview.letter.LetterBaseListAdapter; 8 import com.lixu.letterlistview.letter.LetterListView; 9 import com.lixu.lianxirenlist.R; 10 import android.app.Activity; 11 import android.content.ContentResolver; 12 import android.database.Cursor; 13 import android.graphics.Color; 14 import android.net.Uri; 15 import android.os.Bundle; 16 import android.view.Gravity; 17 import android.view.View; 18 import android.view.ViewGroup; 19 import android.widget.TextView; 20  21 public class MainActivity extends Activity { 22   private ArrayList<String> dataArray; 23  24   @Override 25   protected void onCreate(Bundle savedInstanceState) { 26     super.onCreate(savedInstanceState); 27     setContentView(R.layout.activity_main); 28     // 通过获取手机通讯录的姓名 29     dataArray = new ArrayList<String>(); 30  31     Uri uri = Uri.parse("content://com.android.contacts/contacts"); 32     ContentResolver resolver = this.getContentResolver(); 33     // 给query传递一个SORT_KEY_PRIMARY,让ContentResolver将获得的数据按照联系人名字首字母排序 34     Cursor cursor = resolver.query(uri, null, null, null, 35         android.provider.ContactsContract.Contacts.SORT_KEY_PRIMARY); 36     while (cursor.moveToNext()) 37  38     { 39       // 联系人的id 40       String id = cursor.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts._ID)); 41       // 将联系人按姓名首字母分组 42       String sort_key_primary = cursor 43           .getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts.SORT_KEY_PRIMARY)); 44       // 获取联系人的名字 45       String name = cursor 46           .getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts.DISPLAY_NAME)); 47       dataArray.add(name); 48     } 49  50     LetterListView letterListView = (LetterListView) findViewById(R.id.letterListView); 51     letterListView.setAdapter(new TestAdapter()); 52  53   } 54  55   /** 56    * 这里 使用一个简单的 NameValuePair 对象,做为测试 57    *  58    * @Title: 59    * @Description: 60    * @Author:Justlcw 61    * @Since:2014-5-13 62    * @Version: 63   */ 64   class TestAdapter extends LetterBaseListAdapter<NameValuePair> { 65     /** 字母对应的key,因为字母是要插入到列表中的,为了区别,所有字母的item都使用同一的key. **/ 66     private static final String LETTER_KEY = "letter"; 67  68     public TestAdapter() { 69       super(); 70  71       List<NameValuePair> dataList = new ArrayList<NameValuePair>(); 72       for (int i = 0; i < dataArray.size(); i++) { 73         NameValuePair pair = new BasicNameValuePair(String.valueOf(i), dataArray.get(i)); 74         dataList.add(pair); 75       } 76       setContainerList(dataList); 77     } 78  79     @Override 80     public Object getItem(int position) { 81       return list.get(position); 82     } 83  84     @Override 85     public long getItemId(int position) { 86       return position; 87     } 88  89     @Override 90     public String getItemString(NameValuePair t) { 91       return t.getValue(); 92     } 93  94     @Override 95     public NameValuePair create(char letter) { 96       return new BasicNameValuePair(LETTER_KEY, String.valueOf(letter)); 97     } 98  99     @Override100     public boolean isLetter(NameValuePair t) {101       // 判断是不是字母行,通过key比较,这里是NameValuePair对象,其他对象,就由你自己决定怎么判断了.102       return t.getName().equals(LETTER_KEY);103     }104 105     @Override106     public View getLetterView(int position, View convertView, ViewGroup parent) {107       // 这里是字母的item界面设置.108       if (convertView == null) {109         convertView = new TextView(MainActivity.this);110         ((TextView) convertView).setGravity(Gravity.CENTER_VERTICAL);111         convertView.setBackgroundColor(getResources().getColor(android.R.color.white));112       }113       ((TextView) convertView).setText(list.get(position).getValue());114       ((TextView) convertView).setBackgroundColor(Color.GREEN);115       ((TextView) convertView).setTextSize(25);116       return convertView;117     }118 119     @Override120     public View getContainerView(int position, View convertView, ViewGroup parent) {121       // 这里是其他正常数据的item界面设置.122       if (convertView == null) {123         convertView = new TextView(MainActivity.this);124         ((TextView) convertView).setGravity(Gravity.CENTER_VERTICAL);125       }126       ((TextView) convertView).setText(list.get(position).getValue());127       ((TextView) convertView).setBackgroundColor(Color.YELLOW);128       ((TextView) convertView).setTextSize(20);129 130       return convertView;131     }132   }133 }

 

 1 package com.lixu.letterlistview.letter; 2  3 import android.widget.BaseAdapter; 4  5 /** 6  * 带有侧边字母列表的listView适配器 7  *  8  *@Title: 9  *@Description:10  *@Author:Justlcw11  *@Since:2014-5-812  *@Version:13 */14 public abstract class LetterBaseAdapter extends BaseAdapter15 {16   /** 字母表头部 **/17   protected static final char HEADER = '+';18   /** 字母表尾部 **/19   protected static final char FOOTER = '#';20 21   /**22    * 是否需要隐藏没有匹配到的字母23    * 24    * @return true 隐藏, false 不隐藏25    * @Description:26    * @Author Justlcw27    * @Date 2014-5-828   */29   public abstract boolean hideLetterNotMatch();30   31   /**32    * 获取字母对应的位置33    * 34    * @return position35    * @Description:36    * @Author Justlcw37    * @Date 2014-5-838   */39   public abstract int getIndex(char letter);40 }

 1 package com.lixu.letterlistview.letter; 2  3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 import android.text.TextUtils; 8 import android.util.Log; 9 import android.view.View; 10 import android.view.ViewGroup; 11  12 /** 13  * 通用带有字母列表的泛型对象adapter 14  *  15  * @Title: 16  * @Description: 17  * @Author:Justlcw 18  * @Since:2014-5-9 19  * @Version: 20 */ 21 public abstract class LetterBaseListAdapter<T> extends LetterBaseAdapter { 22   /** log tag. **/ 23   private static final String TAG = "LetterBaseListAdapter"; 24  25   /** 默认错误头部字母. **/ 26   private static final char ERROR_LETTER = ' '; 27  28   /** view type的类型总数 **/ 29   private static final int TYPE_COUNT = 2; 30   /** 字母类型 **/ 31   private static final int TYPE_LETTER = 0; 32   /** 实体类型 **/ 33   private static final int TYPE_CONTAINER = 1; 34  35   /** 添加字母之后的list **/ 36   protected final List<T> list; 37   /** 字母头位置标示map **/ 38   private final Map<Character, Integer> letterMap; 39  40   /** 41    * 构造方法 42   */ 43   public LetterBaseListAdapter() { 44     list = new ArrayList<T>(); 45     letterMap = new HashMap<Character, Integer>(); 46   } 47  48   /** 49    * 构造方法 50    *  51    * @param dataArray 52    *      内容数组 53   */ 54   public LetterBaseListAdapter(T[] dataArray) { 55     this(); 56     setContainerList(dataArray); 57   } 58  59   /** 60    * 构造方法 61    *  62    * @param dataList 63    *      内容列表 64   */ 65   public LetterBaseListAdapter(List<T> dataList) { 66     this(); 67     setContainerList(dataList); 68   } 69  70   /** 71    * 设置主体内容 72    *  73    * @param dataArray 74    *      实体数组 75    * @Description: 76    * @Author Justlcw 77    * @Date 2014-5-9 78   */ 79   protected final void setContainerList(T[] dataArray) { 80     if (!list.isEmpty()) { 81       list.clear(); 82     } 83     if (!letterMap.isEmpty()) { 84       letterMap.clear(); 85     } 86  87     char letter = ERROR_LETTER; 88     int index = 0; 89     for (int i = 0; i < dataArray.length; i++) { 90       T t = dataArray[i]; 91  92       char l = getHeaderLetter(t); 93  94       if (letter != l && l != ERROR_LETTER) { 95         // 如果发现这个字母没有添加过,更新一下标示 96         letter = l; 97         // 创建一个T类型的字母头放进去 98         T tl = create(letter); 99         if (tl != null) {100           // 如果创建成功,则插入到列表中101           list.add(tl);102         }103         // 存放最新字母对应的位置104         letterMap.put(letter, index);105         index++;106       }107       // 添加原本的填充实体项108       list.add(t);109       index++;110     }111   }112 113   /**114    * 设置主体内容.115    * 116    * @param dataList117    *      实体列表118    * @Description:119    * @Author Justlcw120    * @Date 2014-5-9121   */122   protected final void setContainerList(List<T> dataList) {123     if (!list.isEmpty()) {124       list.clear();125     }126     if (!letterMap.isEmpty()) {127       letterMap.clear();128     }129 130     char letter = ' ';131     int index = 0;132     for (int i = 0; i < dataList.size(); i++) {133       T t = dataList.get(i);134 135       char l = getHeaderLetter(t);136 137       if (letter != l && l != ERROR_LETTER) {138         // 如果发现这个字母没有添加过,更新一下标示139         letter = l;140         // 创建一个T类型的字母头放进去141         T tl = create(letter);142         if (tl != null) {143           // 如果创建成功,则插入到列表中144           list.add(tl);145         }146         // 存放最新字母对应的位置147         letterMap.put(letter, index);148         index++;149       }150       // 添加原本的填充实体项151       list.add(t);152       index++;153     }154   }155 156   /**157    * @param t158    *      <实体item对象>159    * 160    * @return <实体item对象> 首字母, 获取失败返回 {@link #ERROR_LETTER}161    * @Description:162    * @Author Justlcw163    * @Date 2014-5-12164   */165   private char getHeaderLetter(T t) {166     // 获取item对应的字符串167     String str = getItemString(t);168     // 如果为空,跳出继续169     if (TextUtils.isEmpty(str)) {170       Log.e(TAG, "item string empty in " + t.toString());171       return ERROR_LETTER;172     }173     char l;174     // 获取第一个字母175     char firstChar = str.charAt(0);176     if (firstChar == HEADER || firstChar == FOOTER || LetterUtil.isLetter(firstChar)) {177       l = firstChar;// 如果是头,尾,字母,直接赋值178     } else {179       String[] letterArray = LetterUtil.getFirstPinyin(firstChar);180       // 如果是汉字,取拼音首字母181       if (letterArray != null && letterArray.length > 0) {182         l = letterArray[0].charAt(0);183       } else {184         // 如果汉字转拼音失败了,跳过185         Log.e(TAG, firstChar + " turn to letter fail, " + t.toString());186         return ERROR_LETTER;187       }188     }189 190     // 如果是小写字母,转换为大写字母191     if (l >= 'a') {192       l = (char) (l - 32);193     }194     return l;195   }196 197   @Override198   public final int getCount() {199     return list.size();200   }201 202   @Override203   public final View getView(int position, View convertView, ViewGroup parent) {204     if (getItemViewType(position) == TYPE_LETTER) {205       return getLetterView(position, convertView, parent);206     }207     return getContainerView(position, convertView, parent);208   }209 210   @Override211   public final int getItemViewType(int position) {212     if (isLetter(list.get(position))) {213       return TYPE_LETTER;214     }215     return TYPE_CONTAINER;216   }217 218   @Override219   public final int getViewTypeCount() {220     return TYPE_COUNT;221   }222 223   @Override224   public boolean hideLetterNotMatch() {225     return false;226   }227 228   @Override229   public final int getIndex(char letter) {230     Integer index = letterMap.get(letter);231     if (index == null) {232       return -1;233     }234     return index;235   }236 237   /**238    * @param T239    *      <实体item对象>240    * 241    * @return <实体item对象>对应的String,用来获取<拼音首字母>242    * @Description:243    * @Author Justlcw244    * @Date 2014-5-9245   */246   public abstract String getItemString(T t);247 248   /**249    * @param letter250    *      <字母>251    * 252    * @return 根据<字母>创建一个<实体item对象>,用来显示<字母item>253    * @Description:254    * @Author Justlcw255    * @Date 2014-5-9256   */257   public abstract T create(char letter);258 259   /**260    * @param t261    *      <实体item对象>262    * 263    * @return 根据<实体item对象>,判断是否是<字母item>264    * @Description:265    * @Author Justlcw266    * @Date 2014-5-9267   */268   public abstract boolean isLetter(T t);269 270   /**271    * 返回 <字母item>界面,其他的同272    * <P>273    * {@link #getView(int, View, ViewGroup)}274    * 275    * @Description:276    * @Author Justlcw277    * @Date 2014-5-9278   */279   public abstract View getLetterView(int position, View convertView, ViewGroup parent);280 281   /**282    * 返回<实体item>界面,其他的同283    * <P>284    * {@link #getView(int, View, ViewGroup)}285    * 286    * @Description:287    * @Author Justlcw288    * @Date 2014-5-9289   */290   public abstract View getContainerView(int position, View convertView, ViewGroup parent);291 }

 1 package com.lixu.letterlistview.letter; 2  3 import java.lang.ref.SoftReference; 4 import java.util.ArrayList; 5 import java.util.List; 6  7 import com.lixu.lianxirenlist.R; 8 import android.content.Context; 9 import android.os.Handler; 10 import android.os.Message; 11 import android.util.AttributeSet; 12 import android.view.Gravity; 13 import android.view.LayoutInflater; 14 import android.view.MotionEvent; 15 import android.view.View; 16 import android.view.ViewGroup; 17 import android.widget.AbsListView; 18 import android.widget.BaseAdapter; 19 import android.widget.FrameLayout; 20 import android.widget.ListView; 21 import android.widget.TextView; 22 import android.widget.AdapterView.OnItemClickListener; 23  24 /** 25  * 带有字母列表的listView 26  *  27  * @Title: 28  * @Description: 29  * @Author:Justlcw 30  * @Since:2014-5-7 31  * @Version: 32 */ 33 public class LetterListView extends FrameLayout { 34   /** 隐藏字母消息 **/ 35   private final int MSG_HIDE_LETTER = 0x0; 36  37   /** 字母列表的宽度 **/ 38   private final int LETTER_LIST_VIEW_WIDTH = 50;// TODO 这里宽度写死了,按着一定的比例比较好 39  40   /** 内容列表 **/ 41   private ListView mListView; 42   /** 内容列表适配器 **/ 43   private LetterBaseAdapter mAdapter; 44  45   /** 字母列表 **/ 46   private ListView mLetterListView; 47   private LetterAdapter mLetterAdapter; 48  49   private TextView mLetterTextView; 50  51   /** 字母消息Handler **/ 52   private Handler mLetterhandler; 53  54   /** 55    * 构造方法 56    *  57    * @param context 58   */ 59   public LetterListView(Context context) { 60     super(context); 61     initListView(context); 62   } 63  64   /** 65    * 构造方法 66    *  67    * @param context 68    * @param attrs 69   */ 70   public LetterListView(Context context, AttributeSet attrs) { 71     super(context, attrs); 72     initListView(context); 73   } 74  75   /** 76    * 初始化 内容列表 字母列表 77    *  78    * @Description: 79    * @Author Justlcw 80    * @Date 2014-5-7 81   */ 82   private void initListView(Context context) { 83     LayoutInflater inflater = LayoutInflater.from(getContext()); 84     // TODO 这里添加内容列表,可以在这里对ListView进行一些你想要的设置 85     mListView = (ListView) inflater.inflate(R.layout.letter_list_container, null, false); 86     LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 87     addView(mListView, lp); 88  89     // TODO 这里添加字母列表,可以在这里对ListView进行一些你想要的设置 90     mLetterListView = (ListView) inflater.inflate(R.layout.letter_list_letter, null, false); 91     mLetterListView.setOnTouchListener(mLetterOnTouchListener); 92     LayoutParams letterListLp = new LayoutParams(LETTER_LIST_VIEW_WIDTH, LayoutParams.MATCH_PARENT, Gravity.RIGHT); 93     addView(mLetterListView, letterListLp); 94  95     // TODO 这里对显示的字母进行设置 96     mLetterTextView = (TextView) inflater.inflate(R.layout.letter_list_position, null, false); 97     LayoutParams letterLp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER); 98     addView(mLetterTextView, letterLp); 99     mLetterTextView.setVisibility(View.INVISIBLE);100 101     // 初始化letter消息发送者102     mLetterhandler = new LetterHandler(this);103   }104 105   /**106    * 设置内容列表适配器107    * 108    * @param adapter109    *      {@link LetterBaseAdapter}110    * @Description:111    * @Author Justlcw112    * @Date 2014-5-7113   */114   public void setAdapter(LetterBaseAdapter adapter) {115     if (adapter != null) {116       mAdapter = adapter;117       mListView.setAdapter(mAdapter);118     }119   }120 121   /**122    * {@link AbsListView#setOnItemClickListener(OnItemClickListener)}123    * 124    * @Description:125    * @Author Justlcw126    * @Date 2014-5-14127   */128   public void setOnItemClickListener(OnItemClickListener onItemClickListener) {129     mListView.setOnItemClickListener(onItemClickListener);130   }131 132   @Override133   protected void onSizeChanged(int w, int h, int oldw, int oldh) {134     super.onSizeChanged(w, h, oldw, oldh);135 136     mLetterAdapter = new LetterAdapter(h - getPaddingTop() - getPaddingBottom());137     mLetterListView.setAdapter(mLetterAdapter);138   }139 140   /**141    * 显示字母142    * 143    * @Description:144    * @Author Justlcw145    * @Date 2014-5-8146   */147   private void showLetter(String letter) {148     if (mLetterTextView.getVisibility() != View.VISIBLE) {149       mLetterTextView.setVisibility(View.VISIBLE);150       mLetterListView.setBackgroundResource(android.R.color.darker_gray);151     }152     mLetterTextView.setText(letter);153 154     mLetterhandler.removeMessages(MSG_HIDE_LETTER);155     mLetterhandler.sendEmptyMessageDelayed(MSG_HIDE_LETTER, 500);156   }157 158   /**159    * 处理消息 {@link LetterHandler#handleMessage(Message)}160    * 161    * @param msg162    *      消息163    * @Description:164    * @Author Justlcw165    * @Date 2014-5-8166   */167   private void handleLetterMessage(Message msg) {168     mLetterTextView.setVisibility(View.INVISIBLE);169     mLetterListView.setBackgroundResource(android.R.color.white);170   }171 172   /** 字母栏touch事件 **/173   private View.OnTouchListener mLetterOnTouchListener = new View.OnTouchListener() {174     @Override175     public boolean onTouch(View v, MotionEvent event) {176       int height = (int) event.getY() - v.getTop();177 178       int position = mLetterAdapter.getTouchPoistion(height);179       if (position >= 0) {180         char letter = (Character) mLetterAdapter.getItem(position);181         // 显示字母182         showLetter(String.valueOf(letter));183 184         // 显示到字母对应的位置185         int select = mAdapter.getIndex(letter);186         if (select >= 0) {187           mListView.setSelection(select);188         }189         return true;190       }191       return false;192     }193   };194 195   /**196    * 字母列表设配器197    * 198    * @Title:199    * @Description:200    * @Author:Justlcw201    * @Since:2014-5-7202    * @Version:203   */204   private class LetterAdapter extends BaseAdapter {205     /** 字母表 **/206     private static final String LETTER_STR = "+ABCDEFGHIJKLMNOPQRSTUVWXYZ#";207     /** 最终显示的字母array **/208     private char[] letterArray;209     /** 每个字母的高度 **/210     private int itemHeight;211 212     /**213      * 构造方法214      * 215      * @param height216      *      view height217     */218     public LetterAdapter(int height) {219       if (mAdapter.hideLetterNotMatch()) {220         List<Character> list = new ArrayList<Character>();221         char[] allArray = LETTER_STR.toCharArray();222         for (int i = 0; i < allArray.length; i++) {223           char letter = allArray[i];224           int position = mAdapter.getIndex(letter);225           if (position >= 0) {226             list.add(letter);227           }228         }229         letterArray = new char[list.size()];230         for (int i = 0; i < list.size(); i++) {231           letterArray[i] = list.get(i);232         }233         list.clear();234         list = null;235       } else {236         letterArray = LETTER_STR.toCharArray();237       }238       itemHeight = height / letterArray.length;239     }240 241     @Override242     public int getCount() {243       return letterArray.length;244     }245 246     @Override247     public Object getItem(int position) {248       return letterArray[position];249     }250 251     @Override252     public long getItemId(int position) {253       return position;254     }255 256     @Override257     public View getView(int position, View convertView, ViewGroup parent) {258       if (convertView == null) {259         convertView = new TextView(getContext());260         ((TextView) convertView).setTextColor(getResources().getColor(android.R.color.black));261         ((TextView) convertView).setGravity(Gravity.CENTER);262         AbsListView.LayoutParams lp = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,263             itemHeight);264         convertView.setLayoutParams(lp);265       }266       ((TextView) convertView).setText(String.valueOf(letterArray[position]));267 268       return convertView;269     }270 271     /**272      * 获取touch的位置273      * 274      * @return position275      * @Description:276      * @Author Justlcw277      * @Date 2014-5-8278     */279     public int getTouchPoistion(int touchHeight) {280       int position = touchHeight / itemHeight;281       if (position >= 0 && position < getCount()) {282         return position;283       }284       return -1;285     }286   }287 288   /**289    * 处理字母显示的handler.290    * 291    * @Title:292    * @Description:293    * @Author:Justlcw294    * @Since:2014-5-8295    * @Version:296   */297   private static class LetterHandler extends Handler {298     /** 弱引用 {@link LetterListView} **/299     private SoftReference<LetterListView> srLetterListView;300 301     /**302      * 构造方法303      * 304      * @param letterListView305      *      {@link LetterListView}306     */307     public LetterHandler(LetterListView letterListView) {308       srLetterListView = new SoftReference<LetterListView>(letterListView);309     }310 311     @Override312     public void handleMessage(Message msg) {313       LetterListView letterListView = srLetterListView.get();314       // 如果view没有被销毁掉,交给view处理这个消息315       if (letterListView != null) {316         letterListView.handleLetterMessage(msg);317       }318     }319   }320 }

 1 package com.lixu.letterlistview.letter; 2  3 import net.sourceforge.pinyin4j.PinyinHelper; 4  5  6 /** 7  * 字母工具类 8  *@Title: 9  *@Description:10  *@Author:Justlcw11  *@Since:2014-5-812  *@Version:13 */14 public class LetterUtil15 {16   /**17    * @param chinese 一个汉字18    * @return 拼音首字母19    * @Description:20    * @Author Justlcw21    * @Date 2014-5-822   */23   public static String[] getFirstPinyin(char chinese)24   {25     return PinyinHelper.toHanyuPinyinStringArray(chinese);26   }27   28   /**29    * 是否是字母30    * 31    * @return true 字母,false 非字母32    * @Description:33    * @Author Justlcw34    * @Date 2014-5-835   */36   public static boolean isLetter(char c)37   {38     return (c >= 65 && c <= 90) || (c >= 97 && c <= 112);39   }40 }

 1 <RelativeLayout ="http://schemas.android.com/apk/res/android" 2   ="http://schemas.android.com/tools" 3   android:layout_width="match_parent" 4   android:layout_height="match_parent" > 5  6   <com.lixu.letterlistview.letter.LetterListView 7     android:id="@+id/letterListView" 8     android:layout_width="match_parent" 9     android:layout_height="match_parent" >10   </com.lixu.letterlistview.letter.LetterListView>11 12 </RelativeLayout>

1 <??>2 <ListView ="http://schemas.android.com/apk/res/android"3   android:layout_width="match_parent"4   android:layout_height="match_parent"5   android:cacheColorHint="@android:color/transparent"6   android:divider="@android:color/transparent"7   android:dividerHeight="0dp"8   android:scrollbars="none" />

1 <??>2 <ListView ="http://schemas.android.com/apk/res/android"3   android:layout_width="match_parent"4   android:layout_height="match_parent"5   android:background="#f44336"6   android:cacheColorHint="@android:color/transparent"7   android:divider="@android:color/transparent"8   android:dividerHeight="0dp"9   android:scrollbars="none" />

 

 1 <??> 2 <TextView ="http://schemas.android.com/apk/res/android" 3   android:layout_width="wrap_content" 4   android:layout_height="wrap_content" 5   android:background="#f44336" 6   android:gravity="center" 7   android:maxWidth="70dip" 8   android:minWidth="70dip" 9   android:padding="10dip"10   android:textColor="@android:color/black"11   android:textSize="50sp" />

运行效果图: