【问题标题】:How do I format a TextView in a custom ListView in Android如何在 Android 的自定义 ListView 中格式化 TextView
【发布时间】:2017-02-22 08:25:44
【问题描述】:

我想显示一个TextView,放置在行的中心,被分隔成两行或多行。 但我只能做

这是我的 custum_adapter.xml 文件

 <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:padding="20dp">



   <TextView
       android:text="magnitude"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/magnitude"
       android:layout_marginLeft="15sp"
       android:textSize="20sp"
       android:layout_alignParentLeft="true"/>
   <TextView
       android:text="place"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:gravity="center_horizontal"
       android:inputType="textMultiLine"
       android:textSize="20sp"
       android:layout_centerHorizontal="true"
       android:id="@+id/place" />

   <TextView
       android:text="date"
       android:inputType="textMultiLine"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/date"
       android:gravity="center_horizontal"
       android:textSize="20sp"
       android:layout_marginRight="15sp"
       android:layout_alignParentRight="true" />
 </RelativeLayout>

我希望我的 UI 看起来像第一张图片。 (我已经看到了这些问题12 和其他一些问题,但没有人能回答我的问题)

编辑:当连续有两个以上的项目时,我曾询问过如何格式化自定义 ListView 中的数据。

【问题讨论】:

    标签: android listview textview


    【解决方案1】:

    您可以只使用具有适当layout_weight 属性的水平LinearLayout 而不是RelativeLayout

    类似这样的:

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:padding="20dp">
    
        <TextView
            android:id="@+id/magnitude"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="magnitude"
            android:textSize="20sp"/>
    
        <TextView
            android:id="@+id/place"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="place"
            android:gravity="center_horizontal"
            android:inputType="textMultiLine"
            android:textSize="20sp"/>
    
        <TextView
            android:id="@+id/date"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="date"
            android:inputType="textMultiLine"
            android:gravity="center_horizontal"
            android:textSize="20sp"/>
    </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      试试这个希望对你有帮助

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical">
      
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="horizontal"
          android:padding="10dp"
          android:weightSum="1">
      
          <TextView
              android:id="@+id/magnitude"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight=".20"
              android:text="magnitude"
              android:textSize="20sp" />
      
          <TextView
              android:id="@+id/place"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight=".50"
              android:inputType="textMultiLine"
              android:text="place"
              android:textSize="20sp" />
      
          <TextView
              android:id="@+id/date"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight=".30"
              android:inputType="textMultiLine"
              android:text="date"
              android:textSize="20sp" />
      </LinearLayout></LinearLayout>
      

      【讨论】:

        【解决方案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:padding="20dp"
        android:orientation="horizontal">
        
        
        <TextView
            android:id="@+id/magnitude"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="magnitude"
            android:textSize="20sp" />
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight=".6"
            android:orientation="horizontal">
        <TextView
            android:id="@+id/place"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:layout_weight="1"
            android:inputType="textMultiLine"
            android:text="place weifjn eiunf indfg dkjmvd jdfokjdfodf"
            android:textSize="20sp" />
        
        <TextView
            android:id="@+id/date"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:inputType="textMultiLine"
            android:text="date"
            android:textSize="20sp" />
        </LinearLayout>
        </LinearLayout>
        

        【讨论】:

          【解决方案4】:

          这应该适合你:

          <?xml version="1.0" encoding="utf-8"?>
          <!-- row.xml -->
          <LinearLayout
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:paddingTop="4dip"
              android:paddingBottom="6dip"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">
          
              <LinearLayout
                  android:orientation="horizontal"
                  android:layout_width="match_parent"
                  android:layout_height="30dp"
          
          
                  <TextView
                     android:text="magnitude"
                      android:layout_width="0dp"
                      android:layout_height="wrap_content"
                      android:layout_weight="0.4"
                      android:gravity="center"
                     android:id="@+id/magnitude"
                     android:layout_marginLeft="15sp"
                     android:textSize="15sp"
                    />
          
                  <TextView
                     android:text="place"
                     android:layout_width="0dp"
                     android:layout_height="wrap_content"
                     android:layout_weight="0.4"
                     android:gravity="center"
                     android:inputType="textMultiLine"
                     android:textSize="15sp"
                     android:id="@+id/place" />
          
                  <TextView
                     android:text="date"
                     android:inputType="textMultiLine"
                     android:layout_width="0dp"
                     android:layout_height="wrap_content"
                     android:layout_weight="0.4"
                     android:id="@+id/date"
                     android:gravity="center"
                     android:textSize="15sp" />
          
          
              </LinearLayout>
          
          </LinearLayout>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2019-02-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多