在ListView中心文 [英] center text in a listview

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

问题描述

我发现它不可能居中的文字在我的列表视图,尝试WRAP_CONTENT和layout_gravity =中心上几乎所有的兽医文不动

这里是我的课agenceEco

 包com.blabla;

进口java.text.SimpleDateFormat的;
进口的java.util.ArrayList;
进口java.util.Date;
进口的java.util.HashMap;
进口的java.util.List;

进口android.app.Activity;

进口android.app.ListActivity;
进口android.content.Intent;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.view.View;
进口android.view.Window;
进口android.widget *。


公共类agenceEco延伸活动{

    ListView控件myList中;


    的String []的listContent = {

            一月,

            二月,

            游行,

            四月,

            可能,

            六月,

            七月,

            八月,

            九月,

            十月,

            十一月,

            十二月

    };

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        的setContentView(R.layout.agence);
        myList上=(ListView控件)findViewById(R.id.liste_agences);



        ArrayAdapter<字符串>适配器

                =新的ArrayAdapter<字符串>(这一点,

                R.layout.simple_list_item,

                的listContent);

        myList.setAdapter(适配器);


        myList.setOnItemClickListener(新AdapterView.OnItemClickListener(){
            公共无效onItemClick(适配器视图<> myAdapter,查看MyView的,INT myItemInt,长mylng){
                字符串selectedFromList =(字符串)(myList.getItemAtPosition(myItemInt));
                Toast.makeText(getBaseContext(),selectedFromList,Toast.LENGTH_SHORT).show();

            }
        });
    }




}
 

继承人simple_list_item.xml

 < TextView中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
          机器人:ID =@机器人:ID / text1中
          机器人:paddingTop =10dip
          机器人:paddingBottom会=10dip
          机器人:layout_width =FILL_PARENT
          机器人:layout_height =FILL_PARENT
          机器人:文字颜色=@彩色/黑白
          机器人:背景=@色/白
          机器人:重力=中心
        />
 

这里的agence.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
              机器人:layout_width =WRAP_CONTENT
              机器人:layout_height =WRAP_CONTENT
              机器人:方向=垂直
              机器人:背景=@色/白
              机器人:layout_gravity =center_horizo​​ntal>
    <的LinearLayout
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:方向=垂直
            机器人:背景=@彩色/黑白>

        < TextView的Andr​​oid的:layout_width =WRAP_CONTENT
                  机器人:layout_height =WRAP_CONTENT
                  机器人:文字颜色=@色/白
                  机器人:文本=
                  机器人:layout_gravity =中心

                />
        < TextView的机器人:ID =@ + ID / txt_date
                  机器人:layout_width =WRAP_CONTENT
                  机器人:layout_height =WRAP_CONTENT
                  机器人:文字颜色=@色/白
                  机器人:文本=Choix DE L'通讯社
                  机器人:layout_gravity =中心
                  机器人:TEXTSTYLE =黑体
                />
        < TextView的Andr​​oid的:layout_width =WRAP_CONTENT
                  机器人:layout_height =WRAP_CONTENT
                  机器人:文字颜色=@彩色/黑白
                  机器人:文本=
                  机器人:layout_gravity =中心

                />
    < / LinearLayout中>
    < TextView的Andr​​oid的:layout_width =WRAP_CONTENT
              机器人:layout_height =WRAP_CONTENT
              机器人:文字颜色=@彩色/黑白
              机器人:文本=
              机器人:layout_gravity =中心

            />
    < ListView的机器人:ID =@ + ID / liste_agences
              机器人:layout_width =WRAP_CONTENT
              机器人:layout_height =WRAP_CONTENT
              机器人:背景=@色/白
              机器人:layout_gravity =center_horizo​​ntal

            >< / ListView控件>


< / LinearLayout中>
 

解决方案

安卓layout_gravity =中心表示到TextView中应该如何放置的父。这是不是你想要的这里,因为一个ListView忽略该属性。与 Android的替代它:比重=中心。这告诉TextView中如何对齐文本里面,在这种情况下,中心吧。

这将没有当你的TextView不填写虽然整个空间效果的(因为该文本将匹配该视图的大小,因此调整不会有太大变化)的。因为你只有一个TextView中的每个条目是有意义的设置既的android:layout_width 安卓layout_height FILL_PARENT


 < TextView中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
          机器人:ID =@机器人:ID / text1中
          机器人:paddingTop =10dip
          机器人:paddingBottom会=10dip
          机器人:layout_width =FILL_PARENT
          机器人:layout_height =FILL_PARENT
          机器人:文字颜色=#FF000000
          机器人:后台=#FFFFFFFF
          机器人:重力=中心
        />
 

I'm finding it impossible to center the text in my listview, tried wrap_content and layout_gravity=center on virtually everything vet the text doesn't move

here's my class agenceEco

package com.blabla;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;

import android.app.ListActivity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.*;


public class agenceEco extends Activity {

    ListView myList;


    String[] listContent = {

            "January",

            "February",

            "March",

            "April",

            "May",

            "June",

            "July",

            "August",

            "September",

            "October",

            "November",

            "December"

    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.agence);
        myList = (ListView)findViewById(R.id.liste_agences);



        ArrayAdapter<String> adapter

                = new ArrayAdapter<String>(this,

                R.layout.simple_list_item,

                listContent);

        myList.setAdapter(adapter);


        myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
                String selectedFromList = (String)(myList.getItemAtPosition(myItemInt));
                Toast.makeText(getBaseContext(),selectedFromList,Toast.LENGTH_SHORT).show();

            }
        });
    }




}

Heres simple_list_item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@android:id/text1"
          android:paddingTop="10dip"
          android:paddingBottom="10dip"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:textColor="@color/black"
          android:background="@color/white"
          android:gravity="center"
        />

here's agence.xml

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              android:background="@color/white"
              android:layout_gravity="center_horizontal">
    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@color/black">

        <TextView android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textColor="@color/white"
                  android:text=" "
                  android:layout_gravity="center"

                />
        <TextView android:id="@+id/txt_date"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textColor="@color/white"
                  android:text="Choix de l'agence"
                  android:layout_gravity="center"
                  android:textStyle="bold"
                />
        <TextView android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textColor="@color/black"
                  android:text=" "
                  android:layout_gravity="center"

                />
    </LinearLayout>
    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textColor="@color/black"
              android:text=" "
              android:layout_gravity="center"

            />
    <ListView android:id="@+id/liste_agences"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:background="@color/white"
              android:layout_gravity="center_horizontal"

            ></ListView>


</LinearLayout>

解决方案

android:layout_gravity="center" indicates to the parent of the textview how it should be placed. This is not what you want here because a ListView ignores that attribute. Replace it with android:gravity="center". This tells the TextView how to align the text inside it, in this case center it.

This will have no effect when your textview doesn't fill the whole space though (since the text will match the size of the view, so align doesn't change much). Since you only have a textview in each entry it makes sense to set both android:layout_width and android:layout_height to fill_parent.


<TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@android:id/text1"
          android:paddingTop="10dip"
          android:paddingBottom="10dip"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:textColor="#ff000000"
          android:background="#ffffffff"
          android:gravity="center"
        />

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

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