2011-01-13 52 views
0

嘿,我想让视图出现在已创建的最后面。如何在下方显示视图

它显示是这样的:

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); 


    } 
} 

回答

0

工作,所以您需要为每个吹气

在你的布局文件夹中,新的XML 布局/ 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> 

然后你布局/ main.xml中创建两个不同的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); 
     } 
    } 

希望它给你的想法。

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文档以获取更多信息。

+0

你指的是`LinearLayout`,而不是`ListView`,对吧?但这实际上并不是这个例子的问题,尽管这是我第一次以为是。 listitem应该只是一个单一的视图,插入另一个垂直的LinearLayout(实际上是一个ListView是需要的)。 – kcoppock 2011-01-13 20:25:49

+0

@kcoppock是LinearLayout不是ListView。现在固定 – 2011-01-13 20:31:48

1

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

+0

你能给我如何实现这一个更明显的例子?我不太明白。我创建ListAdatpter的对象,但是当我尝试实例化它,它说,它不能被insantiated。谢谢。 – rogcg 2011-01-13 20:41:29

0

嗯...我不明白的是需要充分的,但可能是你需要到Android补充:方向=“垂直”来的LinearLayout使用id =“linearId”。

相关问题