【问题标题】:Android: Show 2 TextViews horizontally in each row of a ListViewAndroid:在 ListView 的每一行中水平显示 2 个 TextView
【发布时间】:2013-09-12 16:55:21
【问题描述】:

我正在创建一个自定义 ArrayAdapter,以便在 ListView (Customizing Android ListView Items with Custom ArrayAdapter) 中显示多个 TextView。我的目标是以类似表格的格式(有 2 列)显示它们,有点像这样:

甲乙 ---------- 光盘 ---------- 英法

但是,我一直以垂直格式获取它们,如下所示:

一个 乙 ---------- C D ---------- 乙 F

这是我的活动 xml:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/list_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

这里是 list_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView android:id="@+id/product_name"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip"
    android:textSize="16dip"
    android:textStyle="bold"/>   

<TextView android:id="@+id/status"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip"
    android:textSize="16dip"
    android:textStyle="bold"/>           

 </LinearLayout>

我对 fill_parentwrap_content 不太熟悉,也许我对它们做错了什么?

【问题讨论】:

    标签: android android-listview


    【解决方案1】:

    改变这个

      android:orientation="vertical"
    

      android:orientation="horizontal"
    

    并使用边距作为视图之间的间距。同时进行以下更改

      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
    

    编辑:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    
    <TextView android:id="@+id/product_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:text="A"
        android:textSize="16dip"
        android:textStyle="bold"/>   
    
    <TextView android:id="@+id/status"
        android:layout_marginLeft="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:text="B"
        android:textSize="16dip"
        android:textStyle="bold"/>           
    
     </LinearLayout>
    

    抓拍

    【讨论】:

    • 非常感谢!奇迹般有效! android:layout_marginLeft 成功了。
    • 有没有办法让第二列与左边距保持绝对距离?现在距离第一列是 30dp,对吧?编辑:刚发现,我需要设置第一个TextView的android:width
    • @Karan 是的,您可以根据需要指定边距。或者使用相对布局将第二个文本视图实际放置到第一个文本视图。
    【解决方案2】:

    您已将 android:orientation="vertical" 放入您的行布局中。使其“水平”

    【讨论】:

    • 谢谢!这也有帮助。
    【解决方案3】:

    //按以下方式更改布局:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    
    <TextView android:id="@+id/product_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:textSize="16dip"
        android:textStyle="bold"/>   
    
    <TextView android:id="@+id/status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:textSize="16dip"
        android:textStyle="bold"/>           
    
     </LinearLayout>
    

    【讨论】:

    • android:layout_width="fill_parent" 将使 textview 占据全部空间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    • 2016-06-24
    • 1970-01-01
    相关资源
    最近更新 更多