Proper format for custom ListActivity layouts
I'm trying to add a header to the top of a ListView that is declared in a ListActivity and I am trying to use the method where one includes the ListView inside a LinearLayout and id's the listview as @android:id/list.
I am receiving a java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
after my setContentView() call in the activity. I can't seem to find the problem and I assume it is with formatting or something, can someone help me out?
Here is the xml:
<ListView
android:id="@android:id/l开发者_运维知识库ist"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="0dip">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="224dip"
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_marginRight="20dip">
<TextView
android:id="@+id/toptext"
android:layout_width="fill_parent"
android:singleLine="true"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
android:textStyle="bold"
android:textSize="21sp"
android:textColor="@color/white"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:id="@+id/bottomtext"
android:textSize="19sp"
android:textStyle="italic"
android:maxLines="1"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="100dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="@+id/toprighttext"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="18sp"
android:gravity="top"
android:textColor="@color/blue"
/>
<TextView
android:id="@+id/bottomrighttext"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="18sp"
android:gravity="bottom"
android:textColor="@color/red"
/>
</LinearLayout>
</LinearLayout>
</ListView>
</LinearLayout>
You can get it by having separate header.xml in your layout,add the first three lines of code in your oncreate method in your activity,like below
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.brupress_header);
tv = (TextView) findViewById(R.id.brupress_header_text);
tv.setText("Header");
setContentView(R.layout.list_layout);
I hope it may help you.
精彩评论