Android文档列表查看示例源$ C ​​$ c。在XML [英] Android Documentation List View Example Source Code In XML

查看:114
本文介绍了Android文档列表查看示例源$ C ​​$ c。在XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你看一下关于如何实现一个列表视图和填充它你会发现,他们只教你如何做到这一点在Java中扩展 ListActivity 而不是正常的活动,但使用XML布局文件和扩展将如何在同一code(布局和填充列表)活动

If you look at the Android documentation about how to implement a List View and populate it you will notice that they only teach you how to do it in Java extending ListActivity instead of the normal Activity, but how would the same code (layout and populate the list) using XML layout files and extending Activity?

我问,是因为我想实现一个 RelativeLayout的并添加更多的元素,画面其中的ListView 是具有更大的灵活性比使用 .inflate() addHeaderView() / addFooterView( )

I'm asking this because I want to implement a RelativeLayout and add more elements to the screen where the ListView is with more flexibility than using .inflate() and addHeaderView()/addFooterView().

推荐答案

我认为你正在寻找的方式创建和填充自己的列表。

I think the way you are looking is creating and populating your own list.


  • 创建一个主布局(mainLayout.xml)//它应该有一个线性布局的至少一个。

  • 创建为您的列表行的模板布局。

  • 在活动虚增您的模板循环,填充它,然后把它添加到您的主布局。

下面是一个例子,

listtemplate.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:id="@+id/listLayout"
    android:clickable="true"
    >
            <TextView android:layout_width="wrap_content" 
                      android:textAppearance="?android:attr/textAppearanceLarge" 
                      android:layout_marginLeft="3dp"
                      android:layout_height="wrap_content" 
                      android:layout_alignParentLeft="true"
                      android:layout_alignParentTop ="true"
                      android:id="@+id/mainText" 
                      android:text="TextView">
                </TextView>
</RelativeLayout>

和您的活动,

public void CreateAndAppendListLayout() 
{
   List<String> mainList; //populate it...
   LinearLayout mainLayout = (LinearLayout) findViewById(R.id.mainLayout);
   LayoutInflater li =  (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   for (int i = 0; i < mainList.size();  i++){
       View tempView = li.inflate(R.layout.listtemplate, null);
       TextView textMain = (TextView) tempView.findViewById(R.id.mainText);

       textMain.setText(mainList.get(i)); 
       mainLayout.addView(tempView);
      } 
}

这篇关于Android文档列表查看示例源$ C ​​$ c。在XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆