Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
451 views
in Technique[技术] by (71.8m points)

How to set the text at center in ListView android application?

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you are using built-in layout which is

android.R.layout.simple_list_item_1

this layout cannot be changed. However you can provide your own row layout.

your row should look like this:

<?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" />

remember that the id should be only android:id="@android:id/text1"

and now you can give this to your ArrayAdapter's constructor:

ArrayAdapter<String>(this,R.layout.my_custom_layout,conversionsadd);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...