简体   繁体   English

ListView内的Android布局

[英]Android layout inside ListView

The row of my ListView is made of two TextView s: 我的ListView的行由两个TextView

I have to position the first TextView at the leftmost position and the second TextView at the rightmost position. 我必须将第一个TextView放置在最左侧,将第二个TextView放置在最右侧。 How can I achieve this? 我该如何实现?

(Minimum API support should be at least 2.3 Gingerbread ) (最小的API支持至少应为2.3 Gingerbread

只需简单地创建一个布局xml文件,该文件就会被扩展到每一行,在该布局中,您可以将android:gravity="left"android:gravity="right"用于第一和第二个TextView

您需要实现“自定义列表适配器”,并在Layout中添加带有两个textview的RelativeLayout,其中一个textview的左对齐父级,另一个右对齐。

Best practice it is using RelativeLayout for this purpose 最佳实践是为此目的使用RelativeLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:paddingLeft="16dp"
                android:paddingRight="16dp"
                android:layout_gravity="center"
        >

    <TextView
            android:id="@+id/txtViewTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />

    <TextView
            android:id="@+id/desk"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/txtViewTitle"
            />

</RelativeLayout>

Only the following markup worked for me:(tested on Android 2.3 device) 仅以下标记对我有效:(在Android 2.3设备上测试)

For the 1st TextView: 对于第一个TextView:

android:layout_alignParentLeft="true"
android:layout_alignParentRight="false"

For the 2nd TextView: 对于第二个TextView:

android:layout_alignParentLeft="false"
android:layout_alignParentRight="true"

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

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