【问题标题】:How to set gravity in a linearlayout in recyclerview?如何在recyclerview的线性布局中设置重力?
【发布时间】:2016-08-12 09:56:44
【问题描述】:

我正在使用 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>

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

【问题讨论】:

  • 请贴出你的ViewHolder的代码。
  • 查看 ViewHolder 代码
  • 您希望将文本设置为向右或向左或布局...
  • 我想设置布局左或右
  • 验证 notificationDescriptions.get(position).getIsRecieve() 是否每次都没有返回 true

标签: android xml android-recyclerview android-linearlayout


【解决方案1】:

你的代码很好,但我想试试这个..

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

    试试下面的代码:

    LinearLayout.LayoutParams 参数=(LinearLayout.LayoutParams)holder.linearLayout.getLayoutParams(); params.gravity=重力.LEFT;

    希望它会起作用:)GlbMP

    【讨论】:

      【解决方案3】:

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

      更新:使用Gravity.EndGravity.Start

      【讨论】:

        猜你喜欢
        • 2012-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-20
        相关资源
        最近更新 更多