我想以编程方式设置每个项目的宽度和高度(textview
+ recyleview
),以便无论放置哪个设备,我都可以放置三个项目(当设备不同时,我得到的宽度和高度也就不同)因为密度指标)。我解决此问题的代码如下:
public class ItemRowHolder extends RecyclerView.ViewHolder {
protected TextView playlistTitle;
protected Button playlistMoreButton;
protected RecyclerView playlist_recycler_view_list;
public ItemRowHolder(View view) {
super(view);
this.playlistTitle = (TextView) view.findViewById(R.id.playlist_title);
this.playlist_recycler_view_list = (RecyclerView) view.findViewById(R.id.playlist_recycler_view_list);
this.playlistTitle.measure(0,0);
DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
int screenHeight = (int )displayMetrics.heightPixels;
int titleHeight = this.playlistTitle.getMeasuredHeight();
int rcvHeight = this.playlist_recycler_view_list.getLayoutParams().height;
int layoutHeight = 0;
if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
p.setMargins(5, 20, 5, 20);
view.requestLayout();
}
layoutHeight = (int)(screenHeight - displayMetrics.density* titleHeight-displayMetrics.density*120)/3;
Log.d("MOMOPix","ydpi: "+ displayMetrics.density);
Log.d("MOMOPix","Screen height: "+ screenHeight);
Log.d("MOMOPix","RCV height: "+ rcvHeight);
Log.d("MOMOPix","Title height: "+titleHeight);
Log.d("MOMOPix","Layout height: "+layoutHeight);
this.playlist_recycler_view_list.getLayoutParams().height= layoutHeight;
}
}
layout
如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:selectableItemBackground"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/playlist_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:text="Sample title"
android:textColor="@android:color/white"
android:textSize="18dp" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/playlist_recycler_view_list"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center_vertical"
android:orientation="horizontal" />
</LinearLayout>
对于某些设备,我希望获得结果,对于所有设备,我都希望得到
谢谢。
答案 0 :(得分:0)
您正在以非常奇怪且易于出错的方式进行操作。
如果您要更改项目的布局方式,则应覆盖RecyclerVIew.LayoutManager
(专门用于按名称显示的方式布局视图),而不要在ViewHolder
中执行怪异的测量技巧
要使RecyclerView
在垂直方向上适合三个项目(我假设它是LinearLayoutManager
,您的代码示例中没有提及),我们只需要覆盖LayoutManager
中的一个方法即可:
recyclerView.setLayoutManager(new LinearLayoutManager(context){
@Override
public boolean checkLayoutParams(RecyclerView.LayoutParams lp) {
// force height of viewHolder to be a third of RecyclerView
// this will override layout_height from xml
lp.height = getHeight() /3;
return true;
}
});