web-dev-qa-db-fra.com

Ajuster les articles de recyclage sur Android

J'ai des problèmes! J'ai ce RecyclerView où j'utilise un GridLayoutManager pour réaliser deux colonnes et plusieurs lignes. Mais voici mon problème: j'ai au plus 8 articles dans ce RecyclerView, et je voudrais les adapter en fonction de la taille de l'écran

Jusqu'à présent, j'ai ceci:

enter image description here

en utilisant ce morceau de code:

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

et à l'intérieur constructeur OptionItensAdapter à mon onBindViewHolder:

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

Avez-vous une idée qui me ferait atteindre cet objectif? Merci d'avance.

8
guisantogui

La solution exacte à votre problème est la plus récente Gestionnaire de mise en page flexible pour recycleur

En savoir plus sur la mise en page flexible

Exemple

7
Rahul Devanavar

Jetez un oeil à ce code OnBindViewHolder et modifiez-le selon vos besoins: 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();
        }


    }

Il suffit de poster ce viewHolder pour voir ce que tous les articles.

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

Ma proposition est d'utiliser une mise en page similaire à celle-ci au lieu de RecyclerView, elle tient sur n'importe quel écran. La mise en page fera tout le nécessaire pour s'ajuster d'elle-même sans aucun code.

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

2
Lluis Felisart

Une disposition GridLayout ou Constraint est un bien meilleur choix ici.

Un RecyclerView est (comme son nom l'indique) pour le recyclage - vous devez en utiliser un lorsque vous avez beaucoup de vues/enfants et devez vous assurer que seuls quelques-uns à l'écran utilisent de la mémoire.

Un ConstraintLayout vous permettra à la place d'inclure chaque vue séparément et de définir comment elles sont liées les unes aux autres pour créer le motif de grille.

Un GridLayout comme mon exemple ci-dessous organisera les articles pour vous, sans recyclage.

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

Dans votre code, vous modifiez ensuite la visibilité de l'un de vos 8 boutons que vous souhaitez masquer

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

Si vous souhaitez définir par programme les largeurs (ou hauteurs) des éléments, définissez useDefaultMargins="true" et modifiez les paramètres de mise en page (selon this réponse)

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

Si vous avez corrigé 8 éléments, vous pouvez utiliser LinearLayout et la bibliothèque SDP pour la taille des icônes comme ceci:

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

En utilisant la bibliothèque SDP vous n'avez pas besoin d'écrire un fichier de dimension pour une taille d'écran différente

Captures d'écran: Nexus 4 :

enter image description here

Nexus 5X:

enter image description here

Nexus 6:

enter image description here

0
SANAT

Si vous devez corriger des vues à l'écran, vous n'avez pas besoin de prendre recyclerView. Vous pouvez jouer avec le poids et adapter les objets à l'écran.

Dans votre scénario, vous pouvez suivre le code ci-dessous

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

J'espère que cela fonctionnera pour vous.

0
Mushahid Khatri
@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
Hitesh Shukla

Si votre menu ne change pas dynamiquement, c'est-à-dire que vous avez les paramètres de menu sur l'API, vous n'avez pas besoin d'utiliser Recyclerview ou GridView pour remplir cette disposition. Je préfère combiner LinearLayout (s) avec quelques contraintes pour remplir la disposition statique:

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

Voici le résultat:

enter image description here

0
jemsnaban

Pourquoi utilisez-vous RecyclerView pour cela ??

GridLayout est la meilleure option ici si vous avez un nombre fixe d'éléments. Vous pouvez jouer avec weights d'objet.

Voici un exemple montrant comment adapter 6 LinearLayouts à un écran

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

Vous pouvez ajouter plus d'articles d'une manière similaire

0
Waqas Ahmed Ansari

Je venais de répondre à une question similaire comme celle-ci dans this SO answer link

Fondamentalement, obtenez une taille d'écran, puis ajustez votre taille en conséquence, de sorte que l'essentiel est:

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

Ajoutez une ligne de grille personnalisée et définissez-y la taille, puis définissez l'ajustement automatique qui s'ajustera automatiquement selon l'écran

0
parik dhakan