【问题标题】:Is it possible to center a single item on a listview?是否可以将单个项目集中在列表视图上?
【发布时间】:2015-05-27 15:19:39
【问题描述】:

我在相对布局中有一个自定义列表视图,我想知道是否可以在列表视图中居中单个项目?自定义适配器处理来自 custom_listview.xml 的两个 TextView 字段 p>

For example:
My List view

A    B
C    D
   E
F    G

我在下面包含了我的 xml 布局。

summary.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context="com.myapp.Summary">

        <TextView android:id="@+id/Welcome" android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/mainListView"
            android:layout_below="@+id/Welcome"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="21dp" />

    </RelativeLayout>

    <!-- Side navigation drawer UI -->
    <ListView
        android:id="@+id/navList"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:background="#ffeeeeee"/>

</android.support.v4.widget.DrawerLayout>

custom_listview.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:id="@+id/listView1"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
     tools:context=".LoginActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Please wait.."
        android:layout_weight="1"
        android:id="@+id/txtFieldName" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Please wait.."
        android:id="@+id/txtFieldValue" />
</LinearLayout>

【问题讨论】:

  • 如果您在该特定元素上以编程方式将Gravity 设置为center。如果您使用 ViewHolder 模式,请确保将其他模式设置为默认值。
  • 是的,将项目布局更改为&lt;FrameLayout&gt; &lt;YourCurrentLayout /&gt;&lt;theCenterLayout/&gt;&lt;/FrameLayout&gt; ...现在取决于您的需要隐藏/显示theCenterLayout
  • @EpicPandaForce,请发表您的评论作为答案,我会接受。由于我正在上演帽子戏法,我无法直接将重力设置为中心,因为列表中的所有项目都发生了变化。我正在使用 if 语句来确定何时应用中心重心并且效果很好。 =)

标签: android xml listview android-listview


【解决方案1】:

在您的情况下,最简单的方法是将第二个TextViewVisibility 设置为View.GONE。由于您将TextViews 包装在LinearLayout 中,并且第一个视图具有weight 属性,因此当没有“兄弟”视图时,它将填充整个父布局。

在这种情况下,您必须小心使用if/else 语句重置getView 方法中第二项的可见性。

请记住,在这种情况下,View 将与父级具有相同的宽度 - 即使它写为 wrap_content,它也会填充父级包含的所有水平空间,从而有效地将其呈现为 @987654331 @。

【讨论】:

    【解决方案2】:

    您需要在该特定元素上以编程方式将Gravity 设置为CENTER,以使其在适配器的getView() 方法中居中。如果您使用 ViewHolder 模式,请确保将其他模式设置为默认值;所以你需要一个if/else 声明。

    【讨论】:

      猜你喜欢
      • 2011-04-08
      • 1970-01-01
      • 2011-03-18
      • 1970-01-01
      • 2020-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-25
      相关资源
      最近更新 更多