如何在下面显示视图

时间:2011-01-13 19:43:57

标签: android android-layout android-listview

嘿我想让视图显示在已创建的最后一个视图下方。

显示如下:

alt text

我想让下一个视图显示在最后一个视图下方,因此对于添加的每个新视图,它显示如下。理解?

这是我的代码。 xml文件和java文件。

listitem.xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:id="@+id/linearId" 
    android:padding="6dip"
    >
    <ImageView 
        android:id="@+id/icon" 
        android:layout_width="wrap_content"
        android:layout_height="fill_parent" 
        android:layout_marginRight="6dip"
        android:src="@drawable/icon" 
        />
    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="0dip" android:layout_weight="1"
        android:layout_height="fill_parent"
        >
        <TextView 
            android:id="@+id/txt1" 
            android:layout_width="fill_parent"
            android:layout_height="0dip" 
            android:layout_weight="1"
            android:textSize="20sp" 
            android:gravity="center_vertical"
            android:text="My Application" 
            />
        <TextView 
            android:id="@+id/txt2" 
            android:layout_width="fill_parent"
            android:layout_height="0dip" 
            android:layout_weight="1"
            android:singleLine="true" 
            android:ellipsize="marquee"
            android:text="Simple application that shows how to use RelativeLayout"
            android:textSize="10sp" 
            />
    </LinearLayout>
</LinearLayout>

RatedCalls.java

public class RatedCalls extends Activity {

private static final String LOG_TAG = "RatedCalls";
private TableLayout table;
private CallDataHelper cdh;
private TableRow row;
private TableRow row2;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.listitem);

    Log.i(LOG_TAG, "calling from onCreate()");

    cdh = new CallDataHelper(this);

    startService(new Intent(this, RatedCallsService.class));
    Log.i(LOG_TAG, "Service called.");
    Log.i(LOG_TAG, "before call fillList");

    List<String> ratedCalls = new ArrayList<String>();
    ratedCalls = this.cdh.selectTopCalls();

    LayoutInflater inflater = (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout ll = (LinearLayout) this.findViewById(R.id.linearId);

    for (int i = 0; i < 1; i++) {
        View item = inflater.inflate(R.layout.listitem, null);

        TextView x = (TextView) item.findViewById(R.id.txt1);
        x.setText(ratedCalls.get(0));

        TextView ht = (TextView) item.findViewById(R.id.txt2);
        ht.setText(ratedCalls.get(1));

        ll.addView(item, ViewGroup.LayoutParams.WRAP_CONTENT);


    }
}

4 个答案:

答案 0 :(得分:1)

您的问题是您应该使用列表适配器中的listitem.xml。您正在做的是,首先将内容视图设置为listitem.xml的实例,然后尝试插入listitem INTO listitem的第一个LinearLayout实例。您应该使用listitem.xml作为在自定义适配器的getView()方法中充气的视图。有关示例,请参阅this question中的QuoteAdapter课程,或者只搜索“自定义ArrayAdapter Android”,了解有关此主题的大量结果。

答案 1 :(得分:0)

在您的xml文件中,将android:orientation上的vertical属性设置为<LinearLayout/>

<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent" 
     android:layout_height="?android:attr/listPreferredItemHeight"
     android:id="@+id/linearId" 
     android:padding="6dip"
     android:orientation="vertical"/>

查看LinearLayout课程文档和orientation文档以获取更多信息。

答案 2 :(得分:0)

mmm ...我完全不了解需要什么,但可能需要将android:orientation =“vertical”添加到LinearLayout,id =“linearId”。

答案 3 :(得分:0)

要使用,您需要为每个inflater创建两个不同的xml

在布局文件夹中,新建xml layout / main2.xml

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"    
        android:layout_height="fill_parent">    
             <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="6dip"
            android:src="@drawable/icon"
            />
            <TextView
                android:id="@+id/txt1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="20sp"
                android:gravity="center_vertical"
                android:text="My Application"
                />
            <TextView
                android:id="@+id/txt2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:singleLine="true"
                android:ellipsize="marquee"
                android:text="Simple application that shows how to use RelativeLayout"
                android:textSize="10sp"
                />    
        </LinearLayout>

然后是 layout / main.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/linearId"
    android:orientation="vertical"
    >
    <TextView android:id="@+id/rowCount"    android:layout_width="wrap_content"
    android:layout_height="wrap_content" ></TextView>
     <LinearLayout
         android:id="@+id/linearId2"
       android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip"> 
         </LinearLayout>
</LinearLayout>

然后,您可以设置活动,就像这样

 private int ELEMENTINEACHROW=2 ;
    private int NOOFROW = 4;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout ll = (LinearLayout) this.findViewById(R.id.linearId);
        for (int j = 0; j < NOOFROW; j++) {
            View itemMain = inflater.inflate(R.layout.main, null, false);
            TextView rowCount = (TextView) itemMain.findViewById(R.id.rowCount);
            rowCount.setText("Row:"+(j+1));
            LinearLayout ll2 = (LinearLayout) itemMain.findViewById(R.id.linearId2);
            for (int i = 0; i < ELEMENTINEACHROW; i++) {
                View item = inflater.inflate(R.layout.main2, null, false);

                TextView x = (TextView) item.findViewById(R.id.txt1);
                x.setText("test");

                TextView ht = (TextView) item.findViewById(R.id.txt2);
                ht.setText("good");

                ll2.addView(item, ViewGroup.LayoutParams.FILL_PARENT);
            }
            ll.addView(itemMain, ViewGroup.LayoutParams.FILL_PARENT);
        }
    }

希望它能给你这个想法。