如何在recycleview中创建这种类型的布局?

时间:2019-05-23 07:16:49

标签: android xml android-layout

请建议如何在全视图的recycleview中创建此类布局。

Please suggest how to create this type of layout in recycleview with full height.

1 个答案:

答案 0 :(得分:0)

请尝试以下代码:

  

actvity_layout.xml:

<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:gravity="center"
    android:background="@color/colorBlueBg">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorBlueBg"/>
</LinearLayout>
  

项目布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView android:id="@+id/linear_item_submenu"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="center"
    android:layout_margin="5dp"
    app:cardBackgroundColor="@color/colorBlue"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:padding="50dp">
        <ImageView
            android:id="@+id/item_main_submenu_img"
            android:layout_width="50dp"
            android:layout_height="50dp"/>
        <View android:layout_width="20dp"
            android:layout_height="1dp"
            android:background="#EBCD08"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"/>
        <TextView
            android:id="@+id/item_main_submenu_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="1111"
            android:gravity="center"
            android:textSize="13sp"
            android:textColor="@color/colorWhite"
            android:padding="5dp"
            android:textAllCaps="true"/>
    </LinearLayout>
</android.support.v7.widget.CardView>
  

在Java文件中:

 recyclerview = findViewById(R.id.recyclerview);
        recyclerview.setLayoutManager(new StaggeredGridLayoutManager(2, 1));
        final RecyclerModel[] homeOptionsModel=new RecyclerModel[]{
                new RecyclerModel(R.drawable.ic_android,"FLIGHTS"),
                new RecyclerModel(R.drawable.ic_android,"Hotels"),
                new RecyclerModel(R.drawable.ic_android,"Holodays"),
                new RecyclerModel(R.drawable.ic_android,"deals"),
        };
        recyclerview.setLayoutManager(new StaggeredGridLayoutManager(2, 1));
        RecyclerViewAdapter recyclerViewAdapter=new RecyclerViewAdapter(MainActivity.this, homeOptionsModel);
        recyclerview.setAdapter(recyclerViewAdapter);

代码输出:

enter image description here

希望它对您有用。