[英]android set the size of widget in viewholder,but the content does not show?
I want to fit widget CardView
to all kinds of screens,and my layout is embed CardView
in RecycleView
, I add setLayoutParams()
in ViewHolder
,but after it,my content in CardView
does not show,I don't know how to deal with it,Hope somebody could help me.Here is my code: 我想,以适应小部件CardView
各种屏幕,我的布局嵌入CardView
在RecycleView
,我添加setLayoutParams()
在ViewHolder
,但之后,我的内容CardView
不显示,我不知道该如何处理希望有人可以帮助我。这是我的代码:
cardview.xml: 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>
and my adapter.java: 和我的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());
}
});
}
}
}
Try to do the following: 尝试执行以下操作:
LayoutParams params = holder.itemView.getLayoutParams();
params.height = screenWidth/2;
params.width = screenHeight*24/100;
holder.itemView.setLayoutParams(params);
try this in onCreateViewHolder: 在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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.