【问题标题】:set text of ListView in center - android在中心设置 ListView 的文本 - android
【发布时间】:2014-10-02 16:08:53
【问题描述】:

我尝试在布局的 center 中设置 ListView 的文本。我使用 LinearLayout 并设置 gravity = center,但它总是显示在 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>

在我的代码中,我使用:

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

结果如下: result

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

【问题讨论】:

  • 如果这是答案,请您标记一下吗?谢谢

标签: android android-layout


【解决方案1】:

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

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

然后,在您的代码中,您将不得不这样做

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

希望有所帮助

【讨论】:

    【解决方案2】:
    • layout_gravity = layout 在您的子视图中的影响
    • gravity - content 在您的孩子视图中的影响

    【讨论】:

      猜你喜欢
      • 2012-02-13
      • 2013-12-26
      • 1970-01-01
      • 2016-01-28
      • 1970-01-01
      • 2019-03-23
      • 2016-09-22
      • 2023-04-08
      • 1970-01-01
      相关资源
      最近更新 更多