Pro*_*mar 118 android android-layout android-cardview android-5.0-lollipop android-recyclerview
我有一个包含RecyclerView的片段,其layout_width ="match_parent":
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity$PlaceholderFragment" />
Run Code Online (Sandbox Code Playgroud)
RecyclerView中的项目是CardView,其layout_width ="match_parent":
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
card_view:cardCornerRadius="4dp">
<TextView
android:layout_gravity="center"
android:id="@+id/info_text"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent"
android:textAppearance="?android:textAppearanceLarge"/>
</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)
我将物品视图膨胀如下:
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
CardView v = (CardView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_listitem, null, true);
ViewHolder vh = new ViewHolder(v);
return vh;
}
Run Code Online (Sandbox Code Playgroud)
但是当我运行应用程序时,CardView呈现为wrap_content,如下所示:
请注意,这是在模拟器上运行,而不是在真实设备上运行.
我做错了什么,还是一个错误?
nha*_*man 282
文档inflate
:
从指定的xml资源中扩充新的视图层次结构.如果出现错误,则抛出InflateException.
要加载的XML布局资源的参数
资源 ID(例如,R.layout.main_page)根
视图是生成的层次结构的父级(如果attachToRoot为true),或者只是为root提供一组LayoutParams值的对象返回的层次结构(如果attachToRoot为false.)
attachToRoot 是否应将膨胀的层次结构附加到根参数?如果为false,则root仅用于为XML中的根视图创建LayoutParams的正确子类.返回膨胀层次结构的根视图.如果提供了root并且attachToRoot为true,则为root; 否则它是膨胀的XML文件的根.
重要的是这里不提供真实的,但不提供父:
LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_listitem, parent, false);
Run Code Online (Sandbox Code Playgroud)
提供parent
视图让inflater知道要使用哪些layoutparams.提供false
参数告诉它不要将它附加到父级.这就是你的RecyclerView
意志.
Gan*_*nna 59
使用RelativeLayout作为CardView的直接父级.
<RelativeLayout
android:layout_width="match_parent" ... >
<CardView
android:layout_width="match_parent" ... >
</CardView>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
anu*_*ake 12
这对我有用,
View viewHolder= LayoutInflater.from(parent.getContext())
.inflate(R.layout.item, null, false);
viewHolder.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
return new ViewOffersHolder(viewHolder);
Run Code Online (Sandbox Code Playgroud)
我这样做的方式是:
View view = mInflater.inflate(R.layout.row_cardview, null, true);
WindowManager windowManager = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
int width = windowManager.getDefaultDisplay().getWidth();
view.setLayoutParams(new RecyclerView.LayoutParams(width, RecyclerView.LayoutParams.MATCH_PARENT));
Run Code Online (Sandbox Code Playgroud)
说明: 在获得卡片视图后,在onCreateViewHolde中,获取屏幕宽度,然后相应地设置布局参数以进行卡片视图.
归档时间: |
|
查看次数: |
57779 次 |
最近记录: |