[英]How to set gravity in a linearlayout in recyclerview?
我正在使用 recyclerview,我想在 recyclerview 中向左或向右设置 Linearlayout 的重力。但是我的 Linearlayout 总是在左侧设置,我的代码如下所示。
@Override
public void onBindViewHolder(MessageWindowHolder holder, int position) {
if(notificationDescriptions.get(position).getIsRecieve())
{
holder.linearLayout.setGravity(Gravity.LEFT);
holder.messageTextview.setText(notificationDescriptions.get(position).getMessage());
holder.messageTextview.setBackground(notificationDescriptionActivity.getResources().getDrawable((R.drawable.bubble_a)));
}
else {
holder.linearLayout.setGravity(Gravity.RIGHT);
holder.messageTextview.setText(notificationDescriptions.get(position).getMessage());
holder.messageTextview.setBackground(notificationDescriptionActivity.getResources().getDrawable(R.drawable.bubble_b));
}
}
notification_message.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/singleMessageContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/singleMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dip"
android:background="@drawable/bubble_a"
android:paddingLeft="10dip"
android:text="Hello bubbles!"
android:textColor="@android:color/primary_text_light" />
</LinearLayout>
</LinearLayout>
取景器.java
public class MessageWindowHolder extends RecyclerView.ViewHolder {
protected LinearLayout linearLayout;
protected TextView messageTextview;
public MessageWindowHolder(View view, List<NotificationDescription> notificationDescriptions) {
super(view);
this.linearLayout = (LinearLayout)view.findViewById(R.id.singleMessageContainer);
this.messageTextview = (TextView)view.findViewById(R.id.singleMessage);
}
}
主要布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.mindefy.kidster.activity.NotificationDescriptionActivity"
tools:showIn="@layout/activity_notification_description">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/notificationRecyclerViewParent"
android:layout_centerHorizontal="true"
android:layout_marginBottom="80dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="serif"
android:id="@+id/sendMessageEdittext"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignTop="@+id/sendMessageEdittext"
android:layout_alignParentRight="true"
android:id="@+id/sendMessageReplyButton"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
您的代码很好,但是我想尝试一下。
LayoutParams lp = new LayoutParams();
lp.gravity= Gravity.CENTER_HORIZONTAL;
holder.linearLayout.setLayoutParams(lp);
UPDATE : Another way to do this :
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.weight = 1.0f;
params.gravity = Gravity.CENTER;
holder.linearLayout.setLayoutParams(params);
尝试以下代码:
LinearLayout.LayoutParams params =(LinearLayout.LayoutParams)holder.linearLayout.getLayoutParams(); params.gravity = Gravity.LEFT;
希望它能工作:)GlbMP
SetGravity
的LinearLayout
设置layout_gravity
的LinearLayout
在他的父母。 将singleMessageContainer
宽度从fill_parent
更改为wrap_content
。 这将起作用
更新 :使用Gravity.End
和Gravity.Start
要将视图放置在 RecyclerView 的右侧,您必须将外出聊天消息的布局包装在一个简单的布局中,并将孩子的 layout_gravity 设置为“end”:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/singleMessageContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null">
<TextView
android:id="@+id/singleMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_margin="5dp"
android:background="@drawable/bubble_a"
android:paddingLeft="10dp"
android:text="Hello bubbles!"
android:textColor="@android:color/primary_text_light" />
</FrameLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.