【问题标题】:Fit recyclerview items on android screen在 android 屏幕上适合 recyclerview 项目
【发布时间】:2017-03-09 04:09:42
【问题描述】:

我有麻烦了!我有这个 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);

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

【问题讨论】:

  • 你为什么使用RecyclerView?你没有回收任何东西,因为你没有滚动。使用GridLayout,或TableLayout,或嵌套LinearLayouts,或ConstraintLayout
  • @CommonsWare,我会接受这些组件的一些示例以及它们各自的正确使用方式。

标签: android layout screen


【解决方案1】:

查看此 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);
        }

【讨论】:

    【解决方案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>
    

    【讨论】:

      【解决方案3】:
      @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);
      }
      

      【讨论】:

      • 这不是正确的答案。请仔细阅读问题。
      【解决方案4】:

      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" 并更改布局参数(根据 this 答案)

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

      【讨论】:

        【解决方案5】:

        我刚刚在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;
        }
        

        【讨论】:

        • 好吧,我没有尝试将它放在onBindViewHolder中,但我在屏幕内还有其他组件,我必须减去工具栏、状态栏和默认android按钮的大小,我没有?
        • @guisantogui 哦,没错,你必须减去这些。您需要减去工具栏、状态栏和默认的 android btn,然后除以 8。虽然在获取 android 按钮高度时遇到了问题。有些手机在屏幕上(Sony Experia),有些在屏幕下方(Zenfone)
        【解决方案6】:

        添加自定义网格行并在此处设置大小,然后设置自动调整以根据屏幕自动调整

        【讨论】:

          【解决方案7】:

          你为什么要使用RecyclerView??

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

          这是一个示例,展示如何将 6 个LinearLayouts 放入屏幕

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

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

          【讨论】:

            【解决方案8】:

            如果您的菜单不是动态更改的,即您在 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>
            

            这是结果:

            【讨论】:

              【解决方案9】:

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

              【讨论】:

                【解决方案10】:

                如果您需要将视图修复到 Screen,则不需要使用 recyclerView。您可以使用 Weight 并让项目适合屏幕。

                在您的场景中,您可以按照以下代码进行操作

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

                希望它对你有用。

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2018-09-16
                  • 2021-03-11
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多