如何在gridview android studio中在一个视图中创建2个布局

u4vypkhs  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(546)

我将在另一个布局下使用网格视图制作菜单。我使用recyclerview创建了gridview,但无法显示顶层布局我的代码是
活动\u grid\u layout.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".main.HomeActivity">
  8. <androidx.recyclerview.widget.RecyclerView
  9. android:id="@+id/dataList"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent" />
  12. </androidx.constraintlayout.widget.ConstraintLayout>

网格布局.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="wrap_content"
  6. xmlns:app="http://schemas.android.com/apk/res-auto">
  7. <LinearLayout
  8. android:id="@+id/layoutMenu"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:orientation="vertical"
  12. tools:ignore="MissingConstraints">
  13. <androidx.cardview.widget.CardView
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:layout_marginStart="10sp"
  17. android:layout_marginEnd="10sp"
  18. android:layout_marginTop="20sp"
  19. android:layout_marginBottom="5sp"
  20. android:layout_gravity="center_horizontal"
  21. app:cardCornerRadius="8sp"
  22. app:layout_constraintBottom_toBottomOf="parent"
  23. app:layout_constraintEnd_toEndOf="parent"
  24. app:layout_constraintHorizontal_bias="0.0"
  25. app:layout_constraintStart_toStartOf="parent"
  26. app:layout_constraintTop_toTopOf="parent"
  27. app:layout_constraintVertical_bias="0.0">
  28. <LinearLayout
  29. android:layout_width="match_parent"
  30. android:layout_height="wrap_content"
  31. android:orientation="vertical"
  32. android:paddingTop="20sp"
  33. android:paddingBottom="20sp"
  34. android:gravity="center"
  35. android:layout_gravity="center_vertical|center_horizontal">
  36. <ImageView
  37. android:id="@+id/image_1"
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. android:src="@drawable/ic_home"/>
  41. <TextView
  42. android:id="@+id/text_1"
  43. android:layout_width="wrap_content"
  44. android:layout_height="wrap_content"
  45. android:text="@string/news"
  46. android:textSize="10sp"
  47. android:textStyle="bold"
  48. android:textAlignment="center"/>
  49. </LinearLayout>
  50. </androidx.cardview.widget.CardView>
  51. </LinearLayout>
  52. </androidx.constraintlayout.widget.ConstraintLayout>

结果呢

请帮我解决这个问题,因为我是android开发者的初学者谢谢

5w9g7ksd

5w9g7ksd1#

不包括您的 LinearLayout 内部 grid_layout.xml 把这个放进去 activity_grid_layout.xml 就在recyclerview上面
完成上述操作后,您的布局将发生以下变化:
活动\u grid\u layout.xml

  1. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".main.HomeActivity">
  7. <LinearLayout
  8. android:layout_width="match_parent"
  9. android:layout_height="150dp"
  10. android:id="@+id/layoutImg"
  11. android:padding="20dp"
  12. android:orientation="vertical"
  13. android:background="@color/sunflower_black"
  14. tools:ignore="MissingConstraints">
  15. </LinearLayout>
  16. <androidx.recyclerview.widget.RecyclerView
  17. android:id="@+id/dataList"
  18. android:layout_width="match_parent"
  19. android:layout_height="0dp"
  20. app:layout_constraintTop_toBottomOf="@+id/layoutImg"
  21. app:layout_constraintBottom_toBottomOf="parent"/>
  22. </androidx.constraintlayout.widget.ConstraintLayout>

网格布局.xml:

  1. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="wrap_content">
  6. <LinearLayout
  7. android:id="@+id/layoutMenu"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:orientation="vertical"
  11. tools:ignore="MissingConstraints">
  12. <androidx.cardview.widget.CardView
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:layout_gravity="center_horizontal"
  16. android:layout_marginStart="10sp"
  17. android:layout_marginTop="20sp"
  18. android:layout_marginEnd="10sp"
  19. android:layout_marginBottom="5sp"
  20. app:cardCornerRadius="8sp"
  21. app:layout_constraintBottom_toBottomOf="parent"
  22. app:layout_constraintEnd_toEndOf="parent"
  23. app:layout_constraintHorizontal_bias="0.0"
  24. app:layout_constraintStart_toStartOf="parent"
  25. app:layout_constraintTop_toTopOf="parent"
  26. app:layout_constraintVertical_bias="0.0">
  27. <LinearLayout
  28. android:layout_width="match_parent"
  29. android:layout_height="wrap_content"
  30. android:layout_gravity="center_vertical|center_horizontal"
  31. android:gravity="center"
  32. android:orientation="vertical"
  33. android:paddingTop="20sp"
  34. android:paddingBottom="20sp">
  35. <ImageView
  36. android:id="@+id/image_1"
  37. android:layout_width="wrap_content"
  38. android:layout_height="wrap_content"
  39. android:src="@drawable/ic_home" />
  40. <TextView
  41. android:id="@+id/text_1"
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:text="@string/news"
  45. android:textAlignment="center"
  46. android:textSize="10sp"
  47. android:textStyle="bold" />
  48. </LinearLayout>
  49. </androidx.cardview.widget.CardView>
  50. </LinearLayout>
  51. </androidx.constraintlayout.widget.ConstraintLayout>
展开查看全部

相关问题