如何在 ListView android 应用程序中设置居中的文本? [英] How to set the text at center in ListView android application?
问题描述
我在我的应用程序中在 ListView 上实现了一个应用程序,我使用了 getListView() 方法.我编写的代码如下:
I have implemented an application on ListView in my application i have used getListView() method.I have written code as follows:
String[] conversionsadd = getResources().getStringArray(R.array.conversions);
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,conversionsadd));
getListView().setTextFilterEnabled(true);
getListView().setBackgroundResource(R.drawable.listback2);
当我在 onCreate 方法中编写上述代码时,我可以显示带有一些文本的列表.从上面的代码中,我想在列表的中心显示文本,并以颜色显示文本.
when i write the above code at onCreate method i can able to show the list with some texts. from the above code i would like to display the text in center in the list and also display the text in a color.
推荐答案
您正在使用内置布局
android.R.layout.simple_list_item_1
此布局无法更改.但是,您可以提供自己的行布局.
this layout cannot be changed. However you can provide your own row layout.
您的行应如下所示:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_width="fill_parent"
android:textSize="20sp"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:gravity="center" />
记住id应该只有android:id="@android:id/text1"
现在您可以将其提供给 ArrayAdapter 的构造函数:
and now you can give this to your ArrayAdapter's constructor:
ArrayAdapter<String>(this,R.layout.my_custom_layout,conversionsadd);
这篇关于如何在 ListView android 应用程序中设置居中的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!