Android CardView对齐内部回收视图
我正在使用Android CardView对齐内部回收视图,android,android-layout,Android,Android Layout,我正在使用RecyclerView和LinearLayoutManager,项目是cardwiews。所有卡都有固定的(像素)宽度。如果有必要,可将RecyclerView放置在SwipeRefreshLayout的内部 如何仅使用xml水平对齐每个RecyclerView项?将android:layout\u gravity=“center”应用到卡片上没有效果 与RecyclerView分离: <android.support.v4.widget.SwipeRefreshLayout
RecyclerView
和LinearLayoutManager
,项目是cardwiews
。所有卡都有固定的(像素)宽度。如果有必要,可将RecyclerView放置在SwipeRefreshLayout的内部
如何仅使用xml水平对齐每个RecyclerView
项?将android:layout\u gravity=“center”应用到卡片上没有效果
与RecyclerView分离:
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/news_recycler_view"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:clipToPadding="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.v4.widget.SwipeRefreshLayout>
卡片布局:
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view"
android:layout_height="wrap_content"
style="@style/CardStyle">
...
</android.support.v7.widget.CardView>
...
风格:
<style name="CardStyle" parent="CardView.Light">
...
<item name="android:layout_marginBottom">8dp</item>
<item name="android:layout_width">@dimen/cardview_width</item>
</style>
...
8dp
@尺寸/卡片视图\宽度
尝试使用gravity=“center”将CardView包装在线性布局中
编辑:
我们可以尝试将RecyclerView的子视图封装在线性布局中以实现此效果
我也在我的RecyclerView适配器中为我自己的视图持有者使用了这个技巧(lil代码片段):
尝试使用gravity=“center”将CardView包装在线性布局中
编辑:
我们可以尝试将RecyclerView的子视图封装在线性布局中以实现此效果
我也在我的RecyclerView适配器中为我自己的视图持有者使用了这个技巧(lil代码片段):
谢谢您的回答!我修正了问题中的一个拼写错误;RecyclerView有android:layout\u width=“match\u parent”,所以将RecyclerView包装到LinearLayout中没有效果。嗯,我明白了,我明白了,我的坏,你的卡片视图,它们也有layout\u width=“match\u parent”
吗?不,卡片有固定宽度(比如,layout\u width=200dp)好的,是的,谢谢!我已经投票给你的答案,如果没有人建议一个没有额外布局的解决方案,我会在一段时间内接受它(但感觉没有这样的方式)。谢谢你的答案!我修正了问题中的一个拼写错误;RecyclerView有android:layout\u width=“match\u parent”,所以将RecyclerView包装到LinearLayout中没有效果。嗯,我明白了,我明白了,我的坏,你的卡片视图,它们也有layout\u width=“match\u parent”
吗?不,卡片有固定宽度(比如,layout\u width=200dp)好的,是的,谢谢!我已经把你的答案投了更高的票,如果没有人建议一个没有额外布局的解决方案,我会在一段时间内接受它(但感觉没有这样的方式)。
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/news_recycler_view"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:clipToPadding="false"
android:layout_width="200dp"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:padding="10dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardUseCompatPadding="true"
card_view:cardPreventCornerOverlap="false"
card_view:cardElevation="5dp"
card_view:cardCornerRadius="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:textStyle="bold"
android:padding="10dp"
android:textSize="18sp"
android:gravity="center"
android:background="@drawable/cardview_header"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/question_holder"
android:padding="10dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>