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

安卓開發實作:為自訂 ListView 加上圓角外觀

編輯:關於 Android 開發

預設的 ListView 外觀略顯單調,若想讓它更具設計感,可以透過 shape 定義為列表加上圓角與漸層。以下示範如何實作一個圓角 ListView。

首先在 Activity 中以 ArrayAdapter 綁定一組資料:

 String[] mStrings = { "aaaa", "bbbb", "cccc" }; ListView listView = (ListView) findViewById(R.id.list); listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings));

接著在版面配置中,透過 android:background 指定一個自訂的 shape 資源作為背景:

 <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@layout/list_corner_1" android:layout_weight="1.0" />

重點在於 list_corner_1.xml 這個 shape 定義,用 <corners> 設定四角圓角半徑,並以 <gradient> 加上漸層:

 <?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="https://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:startColor="#CCCCFF" android:endColor="#99CCFF" android:angle="90" /> <corners android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp" android:topLeftRadius="10dp" android:topRightRadius="10dp" /> </shape>

透過 shape 的 corners 與 gradient 標籤,就能輕鬆為 ListView 換上柔和的圓角與漸層外觀,讓列表視覺更精緻。

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