定義佈局文件java
<ListView android:id="@+id/list1" android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="#f00" android:dividerHeight="2px" android:headerDividersEnabled="false" > </ListView>
上面的ListView沒有指定android:entries,意味着要經過adapter來提供列表項.android
2. adapter來提供列表項數組
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView list1 = (ListView)findViewById(R.id.list1); String[] arr1 = {"hello","nihao","你好"}; //將數組包裝成adapter,其中array_items定義文字的樣式 ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,R.layout.array_item,arr1); //爲listView設置adapter list1.setAdapter(adapter1); }
array_item:文本樣式
ide
arr1:爲列表項提供數據佈局
3. 其中array_item指定文本樣式this
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="24dp" android:id="@+id/textView" android:padding="10px" android:shadowColor="#f0f" android:shadowDx="4" android:shadowDy="4" android:shadowRadius="2" > </TextView>