简体   繁体   English

有关listview和R.layout.main的帮助(Android)

[英]help with listview and R.layout.main (Android)

im quite new to android, so i apologise if this is a noob-ish question (: 我对android很陌生,所以如果这是一个菜鸟般的问题,我深表歉意(:

i designed my list following the example found here: http://android-er.blogspot.com/2010/06/custom-arrayadapter-with-with-different.html 我根据此处找到的示例设计了列表: 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? 但是我想知道的是,我将如何添加setContentView(R.layout.main),以便可以与列表一起显示其他(xml)元素?

thanks for any advice (: 感谢您的任何建议(:

You just edit your main.xml to look whatever you want it to look, eg: 您只需编辑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>  

Change your activity to extend only Activity, not ListActivity. 更改您的活动以仅扩展活动,而不扩展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 您创建您的main.xml
  2. Add to it a 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 could be any drawable example: 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" />

Add a layout xmlFile myLayout.xml example: (I added imageView for demonstration) anyway what is important is the id of the textview 添加布局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>

finally in your Activity 终于在您的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM