定义布局文件
<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来提供列表项.
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:文本样式
arr1:为列表项提供数据
3. 其中array_item指定文本样式
<?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>