【问题标题】:center text in a listview在列表视图中居中文本
【发布时间】:2012-02-28 14:32:06
【问题描述】:

我发现无法在我的列表视图中将文本居中,在几乎所有内容上尝试了 wrap_content 和 layout_gravity=center 验证文本不会移动

这是我的班级代理

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

            }
        });
    }




}

这里是 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"
        />

这里是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>

【问题讨论】:

  • 代替包装内容,为文本视图的宽度和高度提供 fill_parent 可能会有所帮助

标签: android listview layout


【解决方案1】:

android:layout_gravity="center" 向 textview 的父级指示它应该如何放置。这不是您想要的,因为 ListView 会忽略该属性。将其替换为android:gravity="center"。这告诉 TextView 如何对齐其中的文本,在这种情况下居中。

当您的 textview 没有填满整个空间时,这将无效(因为文本将匹配视图的大小,因此 align 不会有太大变化)。由于每个条目中只有一个文本视图,因此将 android:layout_widthandroid:layout_height 设置为 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"
        />

【讨论】:

  • 它没有任何效果,在simple_list_item.xml(之前和之后)中显示居中,但是当由listview实现时它向左对齐
  • 我对其进行了测试,并将我的代码添加到了答案中。我改变了提到的属性。除此之外,我删除了可能会引起麻烦的 paddingLeft。虽然没有提到,所以删除后尝试一下。这对我有用(在 Android 4.0.3 上,应该在任何其他版本上)。
  • 也尝试了编辑的版本,但没有更多的运气,我需要更改agence.xml 中的任何内容吗?如果它有助于填充工作,但它不能是固定值 => 不同的屏幕需要不同的值
  • 我在第一篇文章中将我的代码更新为修改不成功后的现在的样子
  • 我不知道是否可以做一些像 padding left 50% 这样的事情,我试过了,但可能是类似的,因为 padding-left 10dp 移动了文本
【解决方案2】:

在您的文本视图中尝试使用 android:layout_centerhorizontal="true" 代替 android:layout_gravity="center"

application name center alignment in android`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-12
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-05
    • 2022-11-03
    相关资源
    最近更新 更多