我想显示一个 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 看起来像第一张图片。 (我已经看过这些问题 1 、 2 和其他一些问题,但没有人能回答我的问题)
编辑:当一行中有两个以上的项目时,我曾询问过有关在自定义 ListView
中格式化数据的问题。
最佳答案
除了 RelativeLayout
,您还可以使用具有适当 layout_weight
属性的水平 LinearLayout
。
像这样:
<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>
关于android - 如何在 Android 的自定义 ListView 中格式化 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40017164/