使用 java 代码将文本视图与父级对齐

align a textview to parent using java code

适配器

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageViewHolder>{
private List<Messages> mMessagesList;
private FirebaseAuth mAuth;

public class MessageViewHolder extends RecyclerView.ViewHolder{
    public TextView messageText;
    public MessageViewHolder(View view)
    {
        super(view);
        messageText = (TextView)view.findViewById(R.id.message_text_layout);
    }
}

public MessageAdapter (List<Messages>mMessagesList)

{
    this.mMessagesList = mMessagesList;
}

@Override
public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
    View V = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_chat_custom,parent,false);
    mAuth = FirebaseAuth.getInstance();
    return new MessageViewHolder(V);
}

@Override
public void onBindViewHolder(MessageViewHolder holder, int position) {

        String current_user_id = mAuth.getInstance().getCurrentUser().getUid();
        Messages messages = mMessagesList.get(position);
        String from_user = messages.getFrom();

    if (from_user!=null && from_user.equals(current_user_id)){
            holder.messageText.setBackgroundResource(R.drawable.text_background1);
            holder.messageText.setTextColor(Color.BLACK);

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.messageText.getLayoutParams();
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        holder.messageText.setLayoutParams(params);

    }else {
            holder.messageText.setBackgroundResource(R.drawable.text_background2);
            holder.messageText.setTextColor(Color.BLACK);

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.messageText.getLayoutParams();
        params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        holder.messageText.setLayoutParams(params);
        }

        holder.messageText.setText(messages.getMessage());
    }

@Override
public  int getItemCount() {
    return mMessagesList.size();
}


public void setMessagesList(List<Messages> mMessagesList) {
    this.mMessagesList = mMessagesList;
}

}

自定义布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/message_single_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp">

<TextView
    android:id="@+id/message_text_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/messages_text_background"
    android:padding="10dp"
    android:text="Textview"
    android:textSize="20dp"
    android:textColor="@color/black"/>

</RelativeLayout>

请帮帮我……我试过使用参数……但它仍然没有给我结果……我从来没有处理过这个问题,因为总是习惯于在 [=21 中对齐它=] 但现在我不能这样做,因为它必须根据条件对齐所以请帮助我......提前致谢......

你有两种可能的方法

第一

移除

android:layout_alignParentStart="true"

来自您的 TextView

第二个 在你的 onBindViewHolder 添加一个 "removeRule" 像这样

params.removeRule(RelativeLayout.ALIGN_PARENT_START);

在添加新规则之前。

这是我测试的代码:

第一

        @Override
        protected void onBindViewHolder(IngredienteViewHolder holder, int position, Ingrediente model) {
            holder.name.setText(model.getName());
            holder.setIngrediente(model);
            if (position % 2 == 0){
                holder.name.setBackgroundColor(Color.BLACK);
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)holder.name.getLayoutParams();
                params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                holder.name.setLayoutParams(params); //causes layout update
            } else {
                holder.name.setBackgroundColor(Color.RED);
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)holder.name.getLayoutParams();
                params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                holder.name.setLayoutParams(params); //causes layout update
            }
        }



<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/list_item_name"
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:textSize="@dimen/list_ingredienti_size"
              android:focusable="false"
              android:text="testo"
              android:textColor="@drawable/selected_textcolor"
              android:gravity="center"
              android:background="@drawable/selected_background"
        />
</RelativeLayout>

第二

    @Override
    protected void onBindViewHolder(IngredienteViewHolder holder, int position, Ingrediente model) {
        holder.name.setText(model.getName());
        holder.setIngrediente(model);
        if (position % 2 == 0){
            holder.name.setBackgroundColor(Color.BLACK);
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)holder.name.getLayoutParams();
            params.removeRule(RelativeLayout.ALIGN_PARENT_START);
            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            holder.name.setLayoutParams(params); //causes layout update
        } else {
            holder.name.setBackgroundColor(Color.RED);
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)holder.name.getLayoutParams();
            params.removeRule(RelativeLayout.ALIGN_PARENT_START);
            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            holder.name.setLayoutParams(params); //causes layout update
        }
    }




<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/list_item_name"
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:layout_alignParentStart="true"
              android:textSize="@dimen/list_ingredienti_size"
              android:focusable="false"
              android:text="testo"
              android:textColor="@drawable/selected_textcolor"
              android:gravity="center"
              android:background="@drawable/selected_background"
        />
</RelativeLayout>