设置的ListView的文本中心 - 机器人 [英] set text of ListView in center - android

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

问题描述

我尝试设置的ListView文本的中心布局。我使用的LinearLayout和设置的比重=中心的,但它总是显示的离开

I try to set text of ListView in center of layout. I use LinearLayout and set gravity = center, but it always shows in left

下面是布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:gravity="center">
        <ListView android:id="@+id/lv_function" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:gravity="center" />
    </LinearLayout>
</LinearLayout>

在我的code,我用:

In my code, I use:

String[] functions = { "Login", "Register", "Setting", "About", "Exit" };
lvMenu = (ListView) findViewById(R.id.lv_function);
ArrayAdapter<String> arr = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, functions);
// set adapter for ListView.
lvMenu.setAdapter(arr);

下面是结果:
结果

你有什么想法?请帮帮我,谢谢!

Do you have any idea? Please help me, thanks!

推荐答案

您必须对您的列表视图项创建自己的布局。 Soemthing这样

You have to create your own layout for your listview item. Soemthing like this

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

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

Then, in your code, your going to have to do this

 new ArrayAdapter<String>(this, R.layout.yourRowItemLayoutHere, R.id.textItem, functions);

希望这有助于

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

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