2017-03-09 142 views
1

我有麻烦了!我有这个RecyclerView,我使用GridLayoutManager来实现两列和几行。 但在这里不用我的问题: 我在这个RecyclerView最多8个项目,并我想根据屏幕尺寸,以适应他们Android屏幕上适合recyclerview项目

到目前为止,我有这样的:

enter image description here

使用这一段代码:

 Rect rectangle = new Rect(); 
     Window window = ((Activity)context).getWindow(); 
     window.getDecorView().getWindowVisibleDisplayFrame(rectangle); 
     int statusBarHeight = rectangle.top; 
     int contentViewTop = 
       window.findViewById(Window.ID_ANDROID_CONTENT).getTop(); 
     int titleBarHeight= contentViewTop - statusBarHeight; 

     final TypedArray styledAttributes = getContext().getTheme().obtainStyledAttributes(
       new int[] { android.R.attr.actionBarSize }); 
     int mActionBarSize = (int) styledAttributes.getDimension(0, 0); 
     styledAttributes.recycle(); 

     int softButtonsHeight = 0; 

     DisplayMetrics metrics = new DisplayMetrics(); 
     ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(metrics); 

     DisplayMetrics realMetrics = new DisplayMetrics(); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 
      ((Activity)context).getWindowManager().getDefaultDisplay().getRealMetrics(realMetrics); 

      if(realMetrics.heightPixels > metrics.heightPixels){ 
       softButtonsHeight = realMetrics.heightPixels - metrics.heightPixels; 
      } 
     } 

     ImageView img_Logo = (ImageView)rootView.findViewById(R.id.img_logo_detalhe); 

     float logoHeight = 0; 
     //convertendo na mão tamanho do sponsor 
     if(img_Logo.getVisibility() != GONE) { 
      logoHeight = 100 * context.getResources().getDisplayMetrics().density; 
     } 

     double sizeInPx = (metrics.heightPixels - titleBarHeight - softButtonsHeight - mActionBarSize - logoHeight)/Math.round(list.size()/2D); 

     itensAdapter = new OptionItensAdapter(context, list, (int)sizeInPx); 
     rvOptions.setAdapter(itensAdapter); 

和内部OptionItensAdapter构造在我的onBindViewHolder

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, sizeInPx); 
     holder.imageButton.setLayoutParams(params); 

你有什么想法可以让我做到这一点吗? 在此先感谢。

+4

新的灵活的布局管理器RecyclerView '?你没有回收任何东西,因为你没有滚动。使用'GridLayout'或'TableLayout',或者嵌套'LinearLayouts'或'ConstraintLayout'。 – CommonsWare

+0

@CommonsWare,我会接受答案这些组件和他们各自的正确方式使用的一些例子。 – guisantogui

回答

0

我刚刚回答了这样一个类似的问题在this SO answer link

基本上得到屏幕大小,然后相应地调整你的身高,所以它的要点是:

DisplayMetrics displayMetrics = new DisplayMetrics(); 
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); 

if(position == 0) { 
    holder.itemView.getLayoutParams().width = displayMetrics.width; 
    holder.itemView.getLayoutParams().height = displayMetrics.height/8; 
} else { 
    holder.itemView.getLayoutParams().width = displayMetrics.width/2; 
    holder.itemView.getLayoutParams().height = displayMetrics.height/8; 
} 
+0

那么,我没有试图把它放在BindViewHolder里面,但是屏幕里面有其他组件,而且我必须减去工具栏,状态栏和默认安卓按钮的大小,我没有? – guisantogui

+0

@guisantogui哦,是的,你得减去那些。你需要减去工具栏,状态栏和默认的android btn,然后除以8.我遇到了问题,虽然在获取android按钮的高度。有些手机在屏幕上(索尼Experia),有些手机在屏幕下方(Zenfone) –

1

一个GridLayoutConstraint布局这里有更好的选择。

A RecyclerView(正如其名称所暗示的)用于回收利用 - 当您有很多视图/孩子并且需要确保只有少数屏幕上正在使用内存时,您应该使用一个。

A ConstraintLayout将允许您分别包含每个视图并定义它们如何相互关联以创建网格模式。

A GridLayout就像我下面的例子将安排物品为你,没有回收。

<GridLayout android:id="@+id/..." 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="fill_horizontal". 
    android:orientation="horizontal" 
    android:columnCount="2" 
    android:rowCount="4"> 

    <OptionItem ... 
     android:weight="1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" /> 
    <OptionItem ... 
     android:weight="1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" /> 
    <OptionItem ... 
     android:weight="1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" /> 
    ... 

</GridLayout> 

在你的代码,然后改变任何你8个按键的知名度,你要隐藏的

button8.setVisibility(View.INVISIBLE); //don't use GONE inside the grid 

如果你想以编程方式设置项的宽度(或高度),设置useDefaultMargins="true"和改变布局PARAMS(按this答案)

GridLayout.LayoutParams params = (GridLayout.LayoutParams) child.getLayoutParams(); 
params.width = (parent.getWidth()/parent.getColumnCount()) -params.rightMargin - params.leftMargin; 
child.setLayoutParams(params); 
0

如果您需要修正意见,屏幕比你不需要采取recyclerView。你可以玩重量和使物品适合屏幕。

在您的情况,你可以按照下面的代码

//llContainer main layout in which you want to put 8 values having orientation vertical 
llContainer.setWeightSum(numberofRaws); // It will be 4 if you want to put 8 values 

for(int i=1; i<=numberofRaws ; i++){ 
    //Inflate One LinearLayout which has height width Match Parent 
    LinearLayout llRaw = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.layout_plain_with_horizontal_orientation, null); 
    llRaw.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARRENT, 1.0f)); 
    AddTwoViewForFaw(llRaw); 

    llContainer.AddView(llRaw); 


} 


public void AddTwoViewForRaw(LinearLayout llRaw){ 

    View v1 = LayoutInflater.from(getContext()).inflate(R.layout.grideLayout, null); 
    // Here you can set values for grid layout by v1.findViewbyId() 
    v1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f)); 
    llRaw.addView(v1); 


    View v2 = LayoutInflater.from(getContext()).inflate(R.layout.grideLayout, null); 
    // Here you can set values for grid layout by v2.findViewbyId() 
    v2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f)); 
    llRaw.addView(v2); 
} 

希望它会为你工作。

0

添加自定义网格行,并设置有大小,然后设置自动调整功能将自动调整为每屏

0

你为什么要使用RecyclerView这个?

GridLayout如果您有固定数量的项目,这里是最好的选择。你可以玩weights的对象。

这里是展示如何适应6 LinearLayout s到屏幕

<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.waqasansari.hitwithme.main.fragments.Dashboard"> 


    <LinearLayout 
     android:id="@+id/myMatches" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     app:layout_columnWeight="1" 
     app:layout_rowWeight="1" 
     app:layout_column="0" 
     app:layout_row="0" 
     android:background="@drawable/border_gray" 
     android:orientation="vertical" 
     android:gravity="center"> 

     <ImageView 
      android:layout_width="80dp" 
      android:layout_height="80dp" 
      android:src="@drawable/dashboard_my_matches"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="My Matches"/> 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/requestMatches" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     app:layout_columnWeight="1" 
     app:layout_rowWeight="1" 
     app:layout_column="1" 
     app:layout_row="0" 
     android:background="@drawable/border_gray" 
     android:gravity="center" 
     android:orientation="vertical"> 

     <ImageView 
      android:layout_width="80dp" 
      android:layout_height="80dp" 
      android:src="@drawable/dashboard_match_requests"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Match Requests"/> 

    </LinearLayout> 


    <LinearLayout 
     android:id="@+id/proShop" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     app:layout_columnWeight="1" 
     app:layout_rowWeight="1" 
     app:layout_column="0" 
     app:layout_row="1" 
     android:background="@drawable/border_gray" 
     android:gravity="center" 
     android:orientation="vertical"> 

     <ImageView 
      android:layout_width="80dp" 
      android:layout_height="80dp" 
      android:src="@drawable/dashboard_pro_shop"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Pro Shops"/> 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/rankings" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     app:layout_columnWeight="1" 
     app:layout_rowWeight="1" 
     app:layout_column="1" 
     app:layout_row="1" 
     android:background="@drawable/border_gray" 
     android:gravity="center" 
     android:orientation="vertical"> 

     <ImageView 
      android:layout_width="80dp" 
      android:layout_height="80dp" 
      android:src="@drawable/dashboard_rankings"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Rankings"/> 

    </LinearLayout> 


    <LinearLayout 
     android:id="@+id/courtsAndCoaches" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     app:layout_columnWeight="1" 
     app:layout_rowWeight="1" 
     app:layout_column="0" 
     app:layout_row="2" 
     android:background="@drawable/border_gray" 
     android:gravity="center" 
     android:orientation="vertical"> 


     <ImageView 
      android:layout_width="80dp" 
      android:layout_height="80dp" 
      android:src="@drawable/dashboard_courts_coaches"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Courts &amp; Coaches"/> 


    </LinearLayout> 


    <LinearLayout 
     android:id="@+id/inviteFriends" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     app:layout_columnWeight="1" 
     app:layout_rowWeight="1" 
     app:layout_column="1" 
     app:layout_row="2" 
     android:background="@drawable/border_gray" 
     android:gravity="center" 
     android:orientation="vertical"> 

     <ImageView 
      android:layout_width="80dp" 
      android:layout_height="80dp" 
      android:src="@drawable/dashboard_invite_friends"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Invite Friends"/> 

    </LinearLayout> 


</android.support.v7.widget.GridLayout> 

您可以用类似的方式

2

添加更多项目看一看这个OnBindViewHolder代码,并改变它为每一个实例您的要求:D

@Override 
    public void onBindViewHolder(ViewHolder viewHolder, final int position) { 

     final int pos = position; 
     try { 
// 
      Resources r = activity.getResources(); 
      int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150, r.getDisplayMetrics()); // i have bottom tabbar so yf you dont have any thing like this just leave 150 to 0.I think in your case height of image view an your top(Pifer) 
      //this change height of rcv 
      DisplayMetrics displaymetrics = new DisplayMetrics(); 
      activity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
      int height = displaymetrics.heightPixels; 
      int width = displaymetrics.widthPixels; 
      RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
      params.height = (height - px)/5; //height recycleviewer (there are 5 rows so divide by 5 but i think in your case there are 4 rows so divide by 4) 
      viewHolder.itemView.setLayoutParams(params); 
      viewHolder.nameTxt.setText(totalList.get(position).getName()); 
      viewHolder.icon.setImageResource(totalList.get(position).getIcon()); 
//   viewHolder.background.setBackground(ContextCompat.getDrawable(context, totalList.get(position).getBackground())); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 


    } 

只需发布此视图,即可查看所有项目。

public static class ViewHolder extends RecyclerView.ViewHolder { 

     public TextView nameTxt; 
     public RelativeLayout background; 
     public ImageView icon; 

     public ViewHolder(View itemLayoutView) { 
      super(itemLayoutView); 

      nameTxt = (TextView) itemLayoutView.findViewById(R.id.menu_label); 
      background = (RelativeLayout) itemLayoutView.findViewById(R.id.menu_background); 
      icon = (ImageView) itemLayoutView.findViewById(R.id.menu_icon); 
     } 
2

我的建议是使用与此相似的布局,而不是RecyclerView,它适用于任何屏幕。布局将完成所有需要自行调整而无需任何代码。

<?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="match_parent" 
    android:orientation="vertical" 
    android:weightSum="100"> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="20" 
     android:src="@android:drawable/sym_def_app_icon" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="20" 
     android:orientation="horizontal" 
     android:weightSum="100"> 

     <ImageView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="50" 
      android:src="@android:drawable/sym_def_app_icon" /> 

     <ImageView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="50" 
      android:src="@android:drawable/sym_def_app_icon" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="20" 
     android:orientation="horizontal" 
     android:weightSum="100"> 

     <ImageView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="50" 
      android:src="@android:drawable/sym_def_app_icon" /> 

     <ImageView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="50" 
      android:src="@android:drawable/sym_def_app_icon" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="20" 
     android:orientation="horizontal" 
     android:weightSum="100"> 

     <ImageView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="50" 
      android:src="@android:drawable/sym_def_app_icon" /> 

     <ImageView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="50" 
      android:src="@android:drawable/sym_def_app_icon" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="20" 
     android:orientation="horizontal" 
     android:weightSum="100"> 

     <ImageView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="50" 
      android:src="@android:drawable/sym_def_app_icon" /> 

     <ImageView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="50" 
      android:src="@android:drawable/sym_def_app_icon" /> 
    </LinearLayout> 


</LinearLayout> 

enter image description here

0

如果你的菜单没有动态变化的,即你对API的菜单设置,则不必使用RecyclerviewGridView填充此布局。我prever到LinearLayout(S)与一些约束相结合,填充静态布局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/content_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="1"> 

    <ImageView 
     android:layout_weight="0.8" 
     android:src="@drawable/logo" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <LinearLayout 
     android:layout_weight="0.2" 
     android:weightSum="1" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <LinearLayout 
      android:weightSum="1" 
      android:layout_weight="0.25" 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <LinearLayout 
       android:gravity="center" 
       android:orientation="vertical" 
       android:layout_weight="0.5" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

       <ImageView 
        android:src="@drawable/free2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" /> 

       <TextView 
        android:gravity="center_horizontal" 
        android:text="this is text" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 

      </LinearLayout> 
      <LinearLayout 
       android:gravity="center" 
       android:orientation="vertical" 
       android:layout_weight="0.5" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

       <ImageView 
        android:src="@drawable/free2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" /> 

       <TextView 
        android:gravity="center_horizontal" 
        android:text="this is text" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 

      </LinearLayout> 

     </LinearLayout> 
     <LinearLayout 
      android:weightSum="1" 
      android:layout_weight="0.25" 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <LinearLayout 
       android:gravity="center" 
       android:orientation="vertical" 
       android:layout_weight="0.5" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

       <ImageView 
        android:src="@drawable/free2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" /> 

       <TextView 
        android:gravity="center_horizontal" 
        android:text="this is text" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 

      </LinearLayout> 
      <LinearLayout 
       android:gravity="center" 
       android:orientation="vertical" 
       android:layout_weight="0.5" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

       <ImageView 
        android:src="@drawable/free2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" /> 

       <TextView 
        android:gravity="center_horizontal" 
        android:text="this is text" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 

      </LinearLayout> 

     </LinearLayout> 
     <LinearLayout 
      android:weightSum="1" 
      android:layout_weight="0.25" 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <LinearLayout 
       android:gravity="center" 
       android:orientation="vertical" 
       android:layout_weight="0.5" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

       <ImageView 
        android:src="@drawable/free2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" /> 

       <TextView 
        android:gravity="center_horizontal" 
        android:text="this is text" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 

      </LinearLayout> 
      <LinearLayout 
       android:gravity="center" 
       android:orientation="vertical" 
       android:layout_weight="0.5" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

       <ImageView 
        android:src="@drawable/free2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" /> 

       <TextView 
        android:gravity="center_horizontal" 
        android:text="this is text" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 

      </LinearLayout> 

     </LinearLayout> 
     <LinearLayout 
      android:weightSum="1" 
      android:layout_weight="0.25" 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <LinearLayout 
       android:gravity="center" 
       android:orientation="vertical" 
       android:layout_weight="0.5" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

       <ImageView 
        android:src="@drawable/free2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" /> 

       <TextView 
        android:gravity="center_horizontal" 
        android:text="this is text" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 

      </LinearLayout> 
      <LinearLayout 
       android:gravity="center" 
       android:orientation="vertical" 
       android:layout_weight="0.5" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

       <ImageView 
        android:src="@drawable/free2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" /> 

       <TextView 
        android:gravity="center_horizontal" 
        android:text="this is text" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 

      </LinearLayout> 

     </LinearLayout> 

    </LinearLayout> 

</LinearLayout> 

这就是结果:

enter image description here

1

如果您有修复8个项目,那么你可以使用LinearLayout和SDP库来获取像这样的图标大小:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context="android.com.linearlayouthome.MainActivity"> 

    <ImageView 
     android:src="@mipmap/ic_launcher" 
     android:layout_width="@dimen/_60sdp" 
     android:layout_height="@dimen/_60sdp" 
     android:layout_gravity="center"/> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:weightSum="4"> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1"> 

      <LinearLayout 
       android:gravity="center" 
       android:orientation="vertical" 
       android:layout_weight="1" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 

       <ImageView 
        android:src="@mipmap/ic_launcher" 
        android:layout_width="@dimen/_70sdp" 
        android:layout_height="@dimen/_70sdp" /> 

       <TextView 
        android:gravity="center_horizontal" 
        android:text="Text" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 

      </LinearLayout> 
      <LinearLayout 
       android:gravity="center" 
       android:orientation="vertical" 
       android:layout_weight="1" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 

       <ImageView 
        android:src="@mipmap/ic_launcher" 
        android:layout_width="@dimen/_70sdp" 
        android:layout_height="@dimen/_70sdp" /> 

       <TextView 
        android:gravity="center_horizontal" 
        android:text="Text" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 

      </LinearLayout> 

     </LinearLayout> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1"> 

      <LinearLayout 
       android:gravity="center" 
       android:orientation="vertical" 
       android:layout_weight="1" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 

       <ImageView 
        android:src="@mipmap/ic_launcher" 
        android:layout_width="@dimen/_70sdp" 
        android:layout_height="@dimen/_70sdp" /> 

       <TextView 
        android:gravity="center_horizontal" 
        android:text="Text" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 

      </LinearLayout> 
      <LinearLayout 
       android:gravity="center" 
       android:orientation="vertical" 
       android:layout_weight="1" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 

       <ImageView 
        android:src="@mipmap/ic_launcher" 
        android:layout_width="@dimen/_70sdp" 
        android:layout_height="@dimen/_70sdp" /> 

       <TextView 
        android:gravity="center_horizontal" 
        android:text="Text" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 

      </LinearLayout> 

     </LinearLayout> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1"> 

      <LinearLayout 
       android:gravity="center" 
       android:orientation="vertical" 
       android:layout_weight="1" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 

       <ImageView 
        android:src="@mipmap/ic_launcher" 
        android:layout_width="@dimen/_70sdp" 
        android:layout_height="@dimen/_70sdp" /> 

       <TextView 
        android:gravity="center_horizontal" 
        android:text="Text" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 

      </LinearLayout> 
      <LinearLayout 
       android:gravity="center" 
       android:orientation="vertical" 
       android:layout_weight="1" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 

       <ImageView 
        android:src="@mipmap/ic_launcher" 
        android:layout_width="@dimen/_70sdp" 
        android:layout_height="@dimen/_70sdp" /> 

       <TextView 
        android:gravity="center_horizontal" 
        android:text="Text" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 

      </LinearLayout> 

     </LinearLayout> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1"> 

      <LinearLayout 
       android:gravity="center" 
       android:orientation="vertical" 
       android:layout_weight="1" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 

       <ImageView 
        android:src="@mipmap/ic_launcher" 
        android:layout_width="@dimen/_70sdp" 
        android:layout_height="@dimen/_70sdp" /> 

       <TextView 
        android:gravity="center_horizontal" 
        android:text="Text" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 

      </LinearLayout> 
      <LinearLayout 
       android:gravity="center" 
       android:orientation="vertical" 
       android:layout_weight="1" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 

       <ImageView 
        android:src="@mipmap/ic_launcher" 
        android:layout_width="@dimen/_70sdp" 
        android:layout_height="@dimen/_70sdp" /> 

       <TextView 
        android:gravity="center_horizontal" 
        android:text="Text" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 

      </LinearLayout> 

     </LinearLayout> 

    </LinearLayout> 

</LinearLayout> 

使用SDP库你不需要编写不同的屏幕尺寸

截图尺寸文件: 的Nexus 4

enter image description here

的Nexus 5X:

enter image description here

的Nexus 6:

enter image description here

0
@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View view = LayoutInflater 
      .from(parent.getContext()) 
      .inflate(R.layout.item_list, null); 

    int height = parent.getMeasuredHeight()/4; 
    int width = parent.getMeasuredWidth(); 

    view.setLayoutParams(new RecyclerView.LayoutParams(width, height)); 

    return new ViewHolder(view); 
} 
+0

这不是正确的答案。请仔细阅读问题。 – Tauqir