绑定视图时更改 textView 的边距

Changing the margins of a textView when binding the view

在我的布局中,我有一个 TextViewRelativeLayout 内部,如下所示:

<?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"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/textView17"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="70dp"
        android:layout_marginStart="70dp"
        android:layout_marginTop="12dp"
        android:layout_toLeftOf="@+id/imageButton2"
        android:layout_toStartOf="@+id/imageButton2"
        android:text="TextView"
        android:textColor="@android:color/black"
        android:textSize="24sp"
        android:textStyle="bold" />

在这种情况下,left margins 明确设置为 70dp。但是,我想有条件地将 left margin 减少到 30dp 左右。我的尝试是在这里:

@Override
public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
    final Material m = ...
    ViewHolder h = (ViewHolder) holder;

    h.checkedImageview.setVisibility(View.GONE);

    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) h.titleTextview.getLayoutParams();
    layoutParams.setMargins(
            20,
            layoutParams.topMargin,
            layoutParams.rightMargin,
            layoutParams.bottomMargin
    );
    h.titleTextview.setLayoutParams(layoutParams);

^^ 然而,那一点作用都没有。如果我修改 XML 布局 中的值,它会正常工作吗?我究竟做错了什么?我正在使用 SectionedRecyclerViewAdapter RecyclerView 库,如果这很重要的话。

例如:

public static class PersonViewHolder extends RecyclerView.ViewHolder {

CardView cv;
TextView personName;
TextView personAge;
ImageView personPhoto;

PersonViewHolder(View itemView) {
    super(itemView);
    cv = (CardView)itemView.findViewById(R.id.cv);
    personName = (TextView)itemView.findViewById(R.id.person_name);
    personAge = (TextView)itemView.findViewById(R.id.person_age);
    personPhoto = (ImageView)itemView.findViewById(R.id.person_photo);
}

}

所以我阅读了 git 中心页面就这样做了:

    @Override
public void onBindItemViewHolder(PersonViewHolder holder, int position) {

  //here --->>>
  PersonViewHolder itemHolder = (PersonViewHolder) holder; // important !

    holder.checkedImageview.setVisibility(View.GONE);

    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) holder.titleTextview.getLayoutParams();
    layoutParams.setMargins(
            20,
            layoutParams.topMargin,
            layoutParams.rightMargin,
            layoutParams.bottomMargin
    );
    holder.titleTextview.setLayoutParams(layoutParams);

问题是您正在重复使用文本视图中的现有 LayoutParams。与其重复使用,不如创建一个新的 RelativeLayout.LayoutParams 对象,设置其边距,然后适当地设置 TextView 的参数。代码:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
    (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

params.setMargins(0, 0, 0, 0); // or any other values

textView.setLayoutParams(params);