在ListView的中心文本项 [英] Center text items in ListView

查看:138
本文介绍了在ListView的中心文本项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在水平我的布局中心对齐的ListView 的文本项?结果
老实说,我用Google搜索至少一个小时,询问这样一个基本问题之前。

感谢。

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直
    工具:上下文=MainActivity。>    <的EditText
        机器人:layout_width =match_parent
        机器人:layout_height =50dp
        机器人:提示=提醒.../>    <的LinearLayout
        机器人:layout_width =match_parent
        机器人:layout_height =200dp
        机器人:baselineAligned =假
        机器人:方向=横向>        < ListView控件
            机器人:ID =@ + ID / listviewTimeInterval
            机器人:layout_width =0dp
            机器人:layout_height =match_parent
            机器人:layout_weight =1
            机器人:滚动条=无/>         &下; - 。 。 - />
         < - 同类的多个列表 - />
         &下; - 。 .-- />    < / LinearLayout中>< / LinearLayout中>


解决方案

您需要为您的列表视图项创建自己的布局,像这​​样

例如:

textcenter.xml:

 <的LinearLayout
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:比重=中心>
    <的TextView
        机器人:ID =@ ID / textItem
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT/>
< / LinearLayout中>

然后,在你的code,你不得不这样做。

  ArrayAdapter<串GT;广告=新ArrayAdapter<串GT;(这一点,R.layout.textcenter,R.id.textItem,功能);
listView.setAdapter(广告);

How can I horizontally center-align text items of the ListView in my Layout?
Honestly, I googled for at least an hour before asking such a basic question.

Thanks.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:hint="Remind..." />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:baselineAligned="false"
        android:orientation="horizontal" >

        <ListView
            android:id="@+id/listviewTimeInterval"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:scrollbars="none" />

         <-- . . . --/>
         <-- more lists of the same kind--/>
         <-- . . .--/>

    </LinearLayout>

</LinearLayout>

解决方案

You need to create your own layout for your listview item, something like this

Example:

textcenter.xml:

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" >
    <TextView
        android:id="@id/textItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

Then, in your code, your have to do this

ArrayAdapter<String> ad = new ArrayAdapter<String>(this, R.layout.textcenter, R.id.textItem, functions);
listView.setAdapter(ad);

这篇关于在ListView的中心文本项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆