如何在recyclerview中的linearlayout中设置重力?

时间:2016-08-12 09:56:44

标签: android xml android-recyclerview android-linearlayout

enter image description here我正在使用recyclerview,我想在recyclerview中左右设置Linearlayout的重力。但我的代码左侧设置的Linearlayout alwayas如下所示。

"use strict";
var mouseTails = document.getElementsByClassName("page-mouse-tail");
document.addEventListener("mousemove", function (event) {
    Array.prototype.forEach.call(mouseTails, function (tail) {
        tail.style.left = event.pageX + "px";
        tail.style.top = event.pageY + "px";
    });
});

notification_message.xml

 @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));
        }

    }

viewholder.java

<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>

主要布局

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);
    }
}

3 个答案:

答案 0 :(得分:1)

SetGravity中的

LinearLayout在其父级中设置了layout_gravity中的LinearLayout。将singleMessageContainer的宽度从fill_parent更改为wrap_content。这会起作用

更新:使用Gravity.EndGravity.Start

答案 1 :(得分:0)

您的代码很好,但我想尝试一下..

   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);

答案 2 :(得分:0)

尝试以下代码:

LinearLayout.LayoutParams params =(LinearLayout.LayoutParams)holder.linearLayout.getLayoutParams(); params.gravity = Gravity.LEFT;

希望它能起作用:) GbMP