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

前端 未结 2 1556
难免孤独
难免孤独 2021-01-29 04:28

im quite new to android, so i apologise if this is a noob-ish question (:

i designed my list following the example found here: http://android-er.blogspot.com/2010/06/cus

相关标签:
2条回答
  • 2021-01-29 04:47

    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.

    0 讨论(0)
  • 2021-01-29 05:05
    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 讨论(0)
提交回复
热议问题