我正在尝试为RecyclerView中的子项设置重力,但似乎LayoutParams没有重力方法。我已经尝试了以下方法:
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)
均能正常工作,并且边距设置正确。我的 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的子项):
<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)
或任何其他可行的方法。谢谢。