【发布时间】: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 模式,请确保将其他模式设置为默认值。 -
是的,将项目布局更改为
<FrameLayout> <YourCurrentLayout /><theCenterLayout/></FrameLayout>...现在取决于您的需要隐藏/显示theCenterLayout -
@EpicPandaForce,请发表您的评论作为答案,我会接受。由于我正在上演帽子戏法,我无法直接将重力设置为中心,因为列表中的所有项目都发生了变化。我正在使用 if 语句来确定何时应用中心重心并且效果很好。 =)
标签: android xml listview android-listview