[英]help with listview and R.layout.main (Android)
我對android很陌生,所以如果這是一個菜鳥般的問題,我深表歉意(:
我根據此處找到的示例設計了列表: http : //android-er.blogspot.com/2010/06/custom-arrayadapter-with-with-different.html
但是我想知道的是,我將如何添加setContentView(R.layout.main),以便可以與列表一起顯示其他(xml)元素?
感謝您的任何建議(:
您只需編輯main.xml使其看起來像您想要的樣子,例如:
<?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>
更改您的活動以僅擴展活動,而不擴展ListActivity。 最后,您可以在活動中使用以下方法獲取列表視圖:
ListView list = (ListView) findViewById(R.id.listview);
並做您需要處理的列表。
向其添加一個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
可以是任何可繪制的示例:
<?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" />
添加布局xmlFile myLayout.xml
示例:(我添加了imageView進行演示)無論如何,重要的是textview的id
<?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>
終於在您的Activity
ArrayAdapter myAD=new ArrayAdapter(mContext,R.layout.myLayout,R.id.thisIsTheTextView,new String[] {"item1", "item2", "item3", "item4", "item5"});
myListView.setAdapter(myAD);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.