我的目标很简单。我想将“红色矩形”作为标题视图添加到ListView
. 所以我创建了一个简单的活动
public class MainActivity extends Activity {
private String[] adapterData = new String[] { "Afghanistan", "Albania", "Algeria",
"American Samoa", "Andorra", "Angola"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv;
lv = (ListView)findViewById(R.id.list);
LayoutInflater lf;
View headerView;
lf = this.getLayoutInflater();
headerView = (View)lf.inflate(R.layout.header, null, false);
lv.addHeaderView(headerView, null, false);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, adapterData));
}
}
activity_main.xml
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0"
android:cacheColorHint="#0000"
android:paddingLeft="20dip"
android:paddingRight="20dip"
android:divider="#0000"
android:scrollbarStyle="outsideOverlay" />
list_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:minHeight="40dip"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
/>
最重要的,header.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dip"
android:background="#F00"/>
但这似乎不起作用。什么都没有显示。
任何人都可以对此提出一些建议吗?非常感谢您的帮助。