繁体   English   中英

在Android屏幕上安装recyclerview项目

[英]Fit recyclerview items on android screen

我有麻烦了! 我有这个RecyclerView,我使用GridLayoutManager来实现两列和几行。 但是我的问题出在这里:我在这个RecyclerView中最多有8个项目, 我想根据屏幕大小来拟合它们

到目前为止,我有这个:

在此输入图像描述

使用这段代码:

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

在我的onBindViewHolder OptionItensAdapter构造函数中:

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

你有什么想法会让我实现这个目标吗? 提前致谢。

针对您的问题的精确解决方案是更新的reyclerView灵活布局管理器

更多关于灵活布局

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


    }

只需发布此viewHolder即可查看所有项目。

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

我的建议是使用类似于此的布局而不是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>

在此输入图像描述

GridLayoutConstraint布局在这里是更好的选择。

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

ConstraintLayout将允许您单独包含每个视图并定义它们彼此之间的关系以创建网格图案。

像我下面的例子一样的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"并更改布局参数(根据答案)

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

这个SO回答链接中,我刚刚回答了类似的问题

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

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

如果您需要将视图修复为屏幕而不需要使用recyclelerView。 您可以玩重量并使物品适合屏幕。

在您的方案中,您可以按照以下代码

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

希望它对你有用。

添加自定义网格行并在那里设置大小,然后设置将根据屏幕自动调整的自动调整

为什么你使用RecyclerView

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

下面是一个示例,说明如何将6个LinearLayout配合到屏幕上

<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>

您可以以类似的方式添加更多项目

如果您的菜单没有动态更改,即您在API上有菜单设置,那么您不必使用RecyclerviewGridView来填充此布局。 我首先将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>

这是结果:

在此输入图像描述

如果您修复了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

在此输入图像描述

Nexus 5X:

在此输入图像描述

Nexus 6:

在此输入图像描述

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM