简体   繁体   English

如何在Android中将ListView文本对齐到右边?

[英]How to Align ListView Text to Right in Android?

I have a list view in an Android application used to display an ArrayList containing Strings and I can't find any properties of the ListView that would allow me to align the text of the ListView Items to the right of it instead of the left. 我在用于显示包含字符串的ArrayList的Android应用程序中有一个列表视图,我找不到ListView的任何属性,这些属性允许我将ListView项的文本对齐到它的右侧而不是左侧。 Any help would be great, thanks. 任何帮助都会很棒,谢谢。

The answer depends on how exactly do you build the list. 答案取决于您如何构建列表。 If you use the the standard ArrayAdapter then you could use the constructor: 如果您使用标准ArrayAdapter那么您可以使用构造函数:

ArrayAdapter<String>(Context, R.layout.aligned_right, values);

where R.layout.aligned_right is a xml layout like this: 其中R.layout.aligned_right是一个像这样的xml布局:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1" 
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"                    
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:paddingLeft="6dip"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="right"/>

This is because these parameters are not defined in the ListView, but in the ListAdapter instead. 这是因为这些参数未在ListView中定义,而是在ListAdapter中定义。 When you specify an adapter for the list, you should set it to return such a layout which aligns items on the right. 为列表指定适配器时,应将其设置为返回对齐右侧项目的布局。

将以下内容添加到布局中

android:layoutDirection="rtl"

For Expandable list view we create two row XMLs namely One is group_row.xml and Other is child_row.xml. 对于可扩展列表视图,我们创建两个行XML,一个是group_row.xml,另一个是child_row.xml。

make group_row.xml as 将group_row.xml设为

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

<TextView
    android:id="@+id/row_group_name"
    android:layout_width="wrap_content"
    android:textSize="24sp"
    android:layout_alignParentRight="true"
    android:textAllCaps="true"
    android:layout_height="wrap_content" />

That is, a Text view aligned to parentRight inside a Relative Layout (which is match parent). 也就是说,Text视图在Relative Layout(与父级匹配)内与parentRight对齐。

After 2 hours of struggling, I achieved this a moment ago. 经过2个小时的挣扎,我刚刚实现了这一目标。

image after 之后的形象 在此输入图像描述

这应该足够了

android:layout_gravity="right" 

Pay attention to one small thing. 注意一件小事。

If you use listitem inside the listview, the gravity attribute of the listitem might not work if the listview layout:width is not set to match_parent . 如果在listview中使用listitem,则如果listview layout:width未设置为match_parent ,则listitem的gravity属性可能不起作用。

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

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