今天逛官网翻文档无意间发现了android:entries属性,其他作用是在布局文件里直接就可以为listview和spinner填充数据,而不用写一句java代码
废话不多说上代码
MainActivity.java :主activity里没有写任何填充数据和adapter的代码
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml 主布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.pullrefresh.MainActivity"
android:orientation="vertical">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/Spinner"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:entries="@array/ListView"/>
</LinearLayout>
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<string-array name="Spinner">
<item>Spinner-1</item>
<item>Spinner-2</item>
<item>Spinner-3</item>
</string-array>
<string-array name="ListView">
<item>ListView-1</item>
<item>ListView-2</item>
<item>ListView-3</item>
<item>ListView-4</item>
<item>ListView-5</item>
<item>ListView-6</item>
<item>ListView-7</item>
</string-array>
</resources>
效果图: