【问题标题】:Listview and textview in the same layoutListview 和 textview 在同一个布局中
【发布时间】:2012-05-28 14:19:43
【问题描述】:

在我的应用程序中,我必须显示一个列表视图,并且在其下方我必须显示一个文本视图。是否可以在同一布局中使用列表视图和文本视图? 我的代码如下:

  setContentView(R.layout.report);  
     ListView lv=(ListView)findViewById(R.id.listView1);
     db.open();
     Cursor c = db.getAllTitles();
     startManagingCursor(c);    
     String[] from = new String[] { db.KEY_INCOME,db.KEY_DESC};
     int[] to = new int[] { R.id.textView1 ,R.id.textView2};
     SimpleCursorAdapter notes =
                new SimpleCursorAdapter(this, R.layout.report, c, from, to);
//     System.out.println("notes="+notes.getCount());
     lv.setAdapter(notes);
    String total=  db.add();   

实际上我正在显示 2 个文本视图以在列表中显示数据,最后我必须添加第 3 个文本视图以显示总计,如果我放置相对布局,此列表视图将正确覆盖另一个。我的 Xml 文件如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >   

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   />
<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   /> 
<ListView
    android:id="@+id/listView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"        
    >
</ListView>  

请任何人帮助我。谢谢。

【问题讨论】:

  • 是的,您可以这样做。你在屏幕上显示什么?你想做什么?
  • 你可以像这样使用自定义适配器ezzylearning.com/…

标签: android listview


【解决方案1】:

是的,但只要列表大小超过屏幕高度,您将永远无法看到 textview。因此,不要将视图添加到 LinearLayout 中,而是将这些视图添加到 RelativeLayout 中,将 ListView 高度设置为填充父视图,以及高于文本视图。和 TextView 在父底部的对齐方式。

【讨论】:

    【解决方案2】:

    您可以在屏幕底部放置一个文本视图,让列表视图占据屏幕顶部和底部文本视图之间的整个空间

    这个布局应该可以解决问题:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">  
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"/>
    
        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_above="@id/textView1"/>
    </RelativeLayout>
    

    【讨论】:

    • 对我不起作用。有人使用此代码成功了吗?
    【解决方案3】:

    是的,您可以在 Listview 下方使用 Textview 使用 Layout margin bottom 为 listview 提供一些填充,并将您的 Textview 添加到 Listview.so 下方以供此使用 RelativeLayout

    【讨论】:

      【解决方案4】:

      使用 Linearlayout 代替 Realitivelayout 并将方向设置为“垂直”

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical" > 
      
      <your textview/>
      <your textview/>   
      
      <your listview/>
      <your textview with proper id/>
      
      </LinearLayout>
      

      【讨论】:

      • 因为我遵循了您的代码,每个数据都出现了总数。但我想要的是总数应该只出现在列表的末尾..
      • 你在问设计还是java逻辑?
      • 对于设计只需将第三个文本视图移动到列表视图的下方。我们无法在您的 java 代码中为您提供帮助,因为我们不知道您的确切要求。
      • 我尝试过它也不起作用..K我将在这里解释我的场景..在我的数据库中,有一个带有cloumnames(收入,描述)的表。在我的布局中,我显示了这个数据库在使用 2 个文本视图的列表视图中(我已经在我的问题中给出了代码)。现在显示列表视图之后,现在我想在文本视图中显示收入列的总数,它应该在列表视图下方。现在我得到了总数,但我不能在 textview 中显示。如果我使用线性布局意味着总计将出现在列表视图中的每个数据上。假设我使用相对布局方式,数据会相互覆盖。
      • 我认为您正在使用带有 2 个文本视图的自定义列表。一个用于收入,另一个用于描述。现在你想要总收入来评估应该在列表视图下方的文本视图。不要在您的 report.xml 文件中使用另一个文本视图。只需在列表视图下方获取一个 textview 并正确获取该 id 并分配您想要的值。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-07
      相关资源
      最近更新 更多