2016-08-12 6 views
1

enter image description here Я работаю с recyclerview, и я хочу установить гравитацию Linearlayout влево или вправо в recyclerview. Но мой Linearlayout alwayas установлен в левом моем коде, показан ниже.Как установить гравитацию в linearlayout в recyclerview?

@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> 
+0

Отправьте код вашего 'ViewHolder', пожалуйста. – earthw0rmjim

+0

check Код ViewHolder –

+0

Вы хотите, чтобы текст был установлен вправо или влево или макет ... – MRX

ответ

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

Try Ниже код:

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

Надеюсь, что это сработает:) GlbMP

Смежные вопросы