由网友(复兴从前)分享简介:我的目标很简单。我想补充一个红色矩形作为的 headerview 的到的ListView 。所以我创建了一个简单的活动My target is simple. I want to add a "Red Rectangle" as a headerview to a ListView.so I create a s...
我的目标很简单。我想补充一个红色矩形作为的 headerview 的到的ListView
。
所以我创建了一个简单的活动
My target is simple. I want to add a "Red Rectangle" as a headerview to a ListView
.
so I create a simple activity
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
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
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
most important, 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"/>
但它似乎并没有工作。不显示任何内容。
But it doesn't seem to work. Nothing is displayed.
可以在任何给这方面的一些建议吗?你的帮助是非常AP preciated。
Can any give some suggestions on this? Your help is highly appreciated.
推荐答案
这是我如何添加一个标题:
This is how I add a header:
View view = View.inflate(context, R.layout.header, null);
lv = (ListView) findViewById(R.id.list);
lv.addHeaderView(view);
相关推荐
最新文章