简体   繁体   English

如何在Android中的自定义ListView中格式化TextView

[英]How do I format a TextView in a custom ListView in Android

I want to show a TextView , placed in center of the row, be separated in two or more lines. 我想显示放置在行中心的TextView ,该行以两行或更多行分隔。 如这里所示 but I am only able to do 但我只能做

这个 .

here is my custum_adapter.xml file 这是我的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>

I want that my UI should look like the fist image. 我希望我的UI看起来像拳头图像。 (I have already seen this these questions 1 , 2 , and a few others but none could answer mine) (我已经看到了这一点这些问题12 ,和其他几个人,但没有人能回答我)

EDIT: I have asked about formatting the data in a custom ListView when there are more than two items in a row. 编辑:当行中有两个以上的项目时,我曾问过有关在自定义ListView格式化数据的问题。

Instead of the RelativeLayout you could just use a horizontal LinearLayout with the appropriate layout_weight attributes. 代替RelativeLayout您可以使用具有适当的layout_weight属性的水平LinearLayout

Something like this: 像这样:

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

Try to this hope this can help you 尝试这个希望可以对您有所帮助

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

Use weights with linearLayout. 将权重与linearLayout一起使用。 It'll solve your problem and they will never overlap . 它会解决您的问题,而且它们永远不会重叠 copy paste the code below. 复制粘贴下面的代码。 You can change the value of weights according to your needs. 您可以根据需要更改权重值。

<?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>

This Should Work For You : 这应该为您工作:

<?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM