簡體   English   中英

如何在recyclerview的線性布局中設置重力?

[英]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

SetGravityLinearLayout設置layout_gravityLinearLayout在他的父母。 singleMessageContainer寬度從fill_parent更改為wrap_content 這將起作用

更新 :使用Gravity.EndGravity.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM