我无法将列表项水平放置在中心位置。当根视图是layout_gravity
时,用center_horizontal
值设置LinearLayout
无效。如果根是RelativeLayout
或ConstraintLayout
,则情况相同。
有人可以告诉我这是怎么回事吗?
下面是fragment's layout和list item's layout。
片段布局
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="@+id/pb_loading_indicator"
android:layout_width="@dimen/progress_bar_size"
android:layout_height="@dimen/progress_bar_size"
android:visibility="invisible"
style="@style/Progress_Bar"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
列表项布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/ListItem_Category_Card">
<TextView
android:id="@+id/list_item_category_title"
style="@style/ListItem_Category_Title"
android:text="Education"
android:layout_gravity="center_horizontal"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/list_item_category_title"
android:layout_centerHorizontal="true">
<ImageView
android:id="@+id/list_item_category_poster"
style="@style/ListItem_Podcast_Image"
android:layout_gravity="top"
android:contentDescription="@string/category_picture_description"
android:src="@drawable/sappfo" />
<TextView
android:id="@+id/list_item_category_explanation"
style="@style/ListItem_Podcast_Title"
android:layout_gravity="bottom"
android:text="@string/lorem_ipsum" />
</android.support.v7.widget.CardView>
</RelativeLayout>