Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

用 ListView 打造圓角表格樣式的清單效果

編輯:關於 Android 程式設計

ListView 除了單調的列表,也能透過客製 Adapter 與背景資源,做出具有視覺層次的表格樣式。本文示範一個只有三筆資料的清單,並依位置套用不同的圓角背景(頂端、中段、底部),營造出連貫的卡片感。

Activity 中的設定:把 divider 設為 null,再掛上自製的 SquareItemAdapter。

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test); ListView list = (ListView)findViewById(R.id.lvLinks); SquareItemAdapter adapter = new SquareItemAdapter(this); list.setDivider(null); list.setAdapter(adapter); } private class SquareItemAdapter extends BaseAdapter { Context context; TextView tvName; public int getCount() { // TODO Auto-generated method stub return 3; } public Object getItem(int position) { // TODO Auto-generated method stub return position; } public long getItemId(int position) { // TODO Auto-generated method stub return position; } public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub final LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); final View v = inflater.inflate(R.layout.square_item_view, null); tvName = (TextView) v.findViewById(R.id.tvItemName); if (position == 0) { tvName.setText(getString(R.string.link_lookaround)); v.setBackgroundResource(R.drawable.circle_list_top); } else if (position == 1) { tvName.setText(getString(R.string.link_recommend)); v.setBackgroundResource(R.drawable.circle_list_middle); } else { tvName.setText(getString(R.string.link_hot)); v.setBackgroundResource(R.drawable.circle_list_bottom); } return v; } public SquareItemAdapter(Context ct) { context = ct; } }

單項目的版面配置(square_item_view.xml):用 RelativeLayout 讓文字靠左、箭頭圖示靠右。

     

文字樣式:可將重複的屬性抽成 style,讓版面更乾淨。

 

若還想加上更細緻的視覺效果,可以再針對背景圖做修飾。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved