绑定视图时更改textView的边距

时间:2017-09-20 14:56:00

标签: android android-relativelayout layoutparams

在我的布局中,TextView内有RelativeLayout,如下所示:

<?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库,如果这很重要的话。

2 个答案:

答案 0 :(得分:0)

例如:

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 hub页面就是这样做的:

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

答案 1 :(得分:0)

问题是您在文本视图中重复使用现有的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);