是否有可能以编程为RecyclerView孩子在Android中设置重力? [英] Is it possible to set gravity programmatically for RecyclerView children in Android?
问题描述
我想设置在RecyclerView儿童重力,但它看起来像的LayoutParams没有重力的方法。我曾尝试以下内容:
I'm trying to set gravity for children in RecyclerView, but it looks like the LayoutParams does not have a gravity method. I have tried the following:
RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(
RecyclerView.LayoutParams.WRAP_CONTENT, RecyclerView.LayoutParams.WRAP_CONTENT);
我也试过如下:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
在这两种情况下 params.setMargin(INT,INT,INT,INT)
工作得很好,并且页边距设置正确。
In both cases params.setMargin(int, int, int, int)
works just fine and the margin is set correctly.
我RecyclerView:
My RecyclerView:
<android.support.v7.widget.RecyclerView
android:id="@+id/messages_list_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"
android:layout_marginTop="20dp"/>
我的RelativeLayout(RecyclerView儿童):
My RelativeLayout (child of RecyclerView):
<RelativeLayout
android:id="@+id/message_text_wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:background="@drawable/chat_bubble"
android:layout_margin="16dp"
android:padding="10dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/message_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textColor="@android:color/primary_text_light"
android:textSize="18sp"
/>
</RelativeLayout>
我希望能够做的就是 params.setGravity(INT)
或任何其他黑客,使这一切成为可能。谢谢你。
What I want to be able to do is params.setGravity(int)
or any other hack that makes this possible. Thanks.
推荐答案
我想换行 RelativeLayout的
在的FrameLayout
与的android:宽=match_parent
,然后设置左/右的重力的的FrameLayout
I would wrap the RelativeLayout
in a FrameLayout
with android:width="match_parent"
and then set the gravity left/right on the FrameLayout
.
这篇关于是否有可能以编程为RecyclerView孩子在Android中设置重力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!