Square Grid - XML

后端 未结 1 507
悲&欢浪女
悲&欢浪女 2021-01-29 09:31

In my studies, I have to do an Android application that retrieves weather data from a weather station. These will be displayed in blocks. These blocks will depart on 4 columns a

相关标签:
1条回答
  • 2021-01-29 09:55

    There are many options : 1. you can choose a recycler view with GridLayoutManager

    <android.support.v7.widget.RecyclerView
      android:id="@+id/main_grid"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:padding="2dp"
      />
    

    So inside activity you will do something like

    mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), COLUMN_COUNT));
    

    2. You choose grid view

    <GridView
    android:id="@+id/grid"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:drawSelectorOnTop="true"
    android:horizontalSpacing="2dp"
    android:numColumns="4"
    android:padding="4dp"
    android:stretchMode="columnWidth"/>
    

    For fixed item size you should use

    <android.support.v7.widget.GridLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:columnCount="4"
      app:orientation="horizontal"
      >
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        />
      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        />
    
    </android.support.v7.widget.GridLayout>
    

    Make sure you add this in your app gradle

    compile 'com.android.support:gridlayout-v7:23.0.1'
    
    0 讨论(0)
提交回复
热议问题