为什么listView addHeaderView()什么都不显示?

时间:2015-07-15 06:46:10

标签: android listview

为什么listView不能自己添加Header?

我已将params添加到标题中而不是WRAP_CONTENT,MATCH_PARETN。

MainActivity:

    public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView list  = (ListView) findViewById(R.id.lv);
        LinearLayout head = (LinearLayout) getLayoutInflater().inflate(R.layout.layout, null);
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(
                200,200
        );
        head.setLayoutParams(params);
        list.addHeaderView(head);
    }
}

activity_main.xml中:

    <RelativeLayout 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=".MainActivity">

    <ListView
        android:id="@+id/lv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</RelativeLayout>

layout.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@mipmap/ic_launcher" />
</LinearLayout>

我该怎么办?

1 个答案:

答案 0 :(得分:0)

尝试从RelativeLayout类而不是AbsListView中输入LayoutParams。我认为这可能是班级演员的问题。