有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java 安卓在viewholder中设置小部件的大小,但内容不显示?

我想把小部件CardView安装到各种屏幕上,我的布局是嵌入CardViewRecycleView中,我在ViewHolder中添加setLayoutParams(),但之后,CardView中的内容没有显示,我不知道如何处理,希望有人能帮助我。这是我的密码:

卡德维尤。xml:

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

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

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

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

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

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

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

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

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

还有我的适配器。爪哇:

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());
            }
        });
    }
}
}

共 (2) 个答案

  1. # 1 楼答案

    尝试执行以下操作:

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

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