【问题标题】:Android -> ListView - How to style 2 textviews to one line?Android -> ListView - 如何将 2 个文本视图设置为一行?
【发布时间】:2014-04-23 23:02:04
【问题描述】:

我有一个列表项,我想在一行中设置 2 个(或更多)文本视图的样式。

------------------------------------------------
|                                              |    
| TEXT_VIEW_1: FOO    TEXT_VIEW_2: BAR         |   
|                                              | 
|                                              |
------------------------------------------------

怎么做最简单有效?

感谢您的任何建议。

【问题讨论】:

  • 尝试设置最小宽度和最大宽度。根据您的需要。
  • 您希望动态添加 TextView,还是始终知道确切的数字?你所说的“风格”到底是什么意思?
  • 设置 android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:maxWidth="300dp"

标签: android listview


【解决方案1】:

如下创建列表项布局...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="FOO" />

     <TextView
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="BAR" />

</LinearLayout>

如果你想拥有超过 2 个TextView,那么只需向它们添加android:layout_weight="1" 属性。如果所有TextView 都包含android:layout_weight="1",则此属性会将布局宽度平均分配给所有Textviews

【讨论】:

  • 通过设置权重可能会影响文本视图的内容。一个是长文本,另一个是小文本,可能会影响视图。
  • 如果android:layout_weight="1" 设置为所有TextView 那么long 不会造成任何问题。
  • 它会尝试用大文本填充第一个文本视图,用小文本填充第二个文本视图。还有一件事,它是一个列表视图,所以我们需要以对齐的方式显示列表。
  • 首先,父LinearLayout将收集所有weight,然后将其width除以weight的总和,然后将除结果设置为所有TextViewslayout_width.
【解决方案2】:

使用 LinearLayout 的方向为水平:

并提供权重:

<?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"
    android:weightSum="3" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Text1"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Text2"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Text3"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

【讨论】:

    【解决方案3】:

    使用下面的代码作为列表项。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightsum="2" >
    
    <TextView
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="FOO" />
    
     <TextView
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="BAR" />
    
    </LinearLayout> 
    

    并使两个 textview singleLine 都为 true。

    【讨论】:

      【解决方案4】:

      这一切都在这个线程中进行了解释。您只需要修改文本视图并排的 XML 即可:

      Android custom Row Item for ListView

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-20
        • 1970-01-01
        • 1970-01-01
        • 2013-04-17
        • 2015-10-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多