开发者

help with listview and R.layout.main (Android)

im quite new to android, so i apologise if this 开发者_StackOverflow社区is a noob-ish question (:

i designed my list following the example found here: http://android-er.blogspot.com/2010/06/custom-arrayadapter-with-with-different.html

but what i would like to know is how would i go about adding setContentView(R.layout.main) so that i can display other (xml) elements along with the list?

thanks for any advice (:


You just edit your main.xml to look whatever you want it to look, eg:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
  <View android:id="@+id/emptyview"
    android:layout_height="30dp"
    android:layout_width="fill_parent" 
  />
  <ListView android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:drawSelectorOnTop="true"
    android:stackFromBottom="true"
    android:layout_below="@id/emptyview"
    android:headerDividersEnabled="true"
  />
</RelativeLayout>  

Change your activity to extend only Activity, not ListActivity. Finally, in your activity you can then fetch your list view with:

ListView list = (ListView) findViewById(R.id.listview);

and do whatever you need to do with the list.


  1. You create your main.xml
  2. Add to it a ListView

    <ListView android:layout_width="match_parent"
    android:layout_height="match_parent" android:id="@+id/myListView"
    android:divider="#ffa500" android:dividerHeight="1px"
    android:background="@drawable/somedrawable_xml"        
    android:choiceMode="singleChoice"></ListView>
    

somedrawable_xml.xml could be any drawable example:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#00ffa500" />
<stroke android:width="2dp" android:color="#ffffa500" />
<padding android:left="1dp" android:top="1dp" android:right="1dp"
    android:bottom="1dp" />

Add a layout xmlFile myLayout.xml example: (I added imageView for demonstration) anyway what is important is the id of the textview

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/dasdasd">
    <TextView android:text="TextView" android:layout_height="wrap_content"             
    android:id="@+id/thisIsTheTextView" android:layout_width="wrap_content"     android:textAppearance="?    
    android:attr/textAppearanceLarge"></TextView>
<ImageView android:src="@drawable/icon" android:id="@+id/imageView1" android:layout_width="wrap_content" 
    android:layout_height="wrap_content" android:layout_alignParentRight="true"></ImageView>

finally in your Activity

ArrayAdapter myAD=new ArrayAdapter(mContext,R.layout.myLayout,R.id.thisIsTheTextView,new String[] {"item1", "item2", "item3", "item4", "item5"});
myListView.setAdapter(myAD);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜