android 在viewholder 中设置widget 的大小,但是内容不显示?

android set the size of widget in viewholder,but the content does not show?

我想让小部件CardView适应各种屏幕,我的布局是嵌入CardViewRecycleView,我在[=17=中添加setLayoutParams() ],但是之后,我在CardView里面的内容没有显示,我不知道怎么处理,希望有人能帮忙me.Here是我的代码:

cardview.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cv_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground">

<LinearLayout
    android:id="@+id/layoutView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/texttitle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textColor="@color/black"
        android:textSize="25dp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="9dp" />

    <TextView
        android:id="@+id/textcontent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textColor="@color/black" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="20dp" />

    <View
        android:id="@+id/colorfulview"
        android:layout_width="20dp"
        android:layout_height="5dp"
        android:layout_gravity="center_horizontal"
        android:background="@color/black"></View>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="18dp" />
</LinearLayout>

和我的 adapter.java:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.TextViewHolder> {
private final LayoutInflater mLayoutInflater;
private final Context mContext;
private int mScreenHeight, mScreenWidth;
private String[] mTitles;
private String[] mContents;
private int[] mColors = {R.color.qing, R.color.zi, R.color.fenhong, R.color.juhuang, R.color.qianzi, R.color.lan};

public RecyclerViewAdapter(Context context, int screenHeight, int screenWidth) {
    mTitles = context.getResources().getStringArray(R.array.titles);
    mContents = context.getResources().getStringArray(R.array.contents);
    mContext = context;
    mLayoutInflater = LayoutInflater.from(context);
    mScreenHeight = screenHeight;
    mScreenWidth = screenWidth;
}

@Override
public TextViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new TextViewHolder(mLayoutInflater.inflate(R.layout.item_main_recycler, parent, false));
}

@Override
public void onBindViewHolder(TextViewHolder holder, int position) {
    holder.mTextTitle.setText(mTitles[position]);
    holder.mTextContent.setText(mContents[position]);
    holder.mColorfulView.setBackgroundResource(mColors[position]);
    holder.mLayoutView.setLayoutParams(new CardView.LayoutParams(mScreenWidth / 2, (int) (mScreenHeight * 0.24)));
}

@Override
public int getItemCount() {
    return mTitles == null ? 0 : mTitles.length;
}

public static class TextViewHolder extends RecyclerView.ViewHolder {
    @InjectView(R.id.texttitle)
    TextView mTextTitle;
    @InjectView(R.id.textcontent)
    TextView mTextContent;
    @InjectView(R.id.colorfulview)
    View mColorfulView;
    @InjectView(R.id.layoutView)
    LinearLayout mLayoutView;

    TextViewHolder(View view) {
        super(view);
        ButterKnife.inject(this, view);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("NormalTextViewHolder", "onClick--> position = " + getPosition());
            }
        });
    }
}
}

尝试执行以下操作:

            LayoutParams params = holder.itemView.getLayoutParams();
            params.height = screenWidth/2;
            params.width = screenHeight*24/100;
            holder.itemView.setLayoutParams(params);

在 onCreateViewHolder 中试试这个:

    View itemView = mLayoutInflater.inflate(R.layout.view_item, parent, false);
    int height = screenWidth/2;
    int width = screenHeight*24/100;
    itemView.setMinimumHeight(height);
    itemView.setMinimumWidth(width );
    return new TextViewHolder(itemView);