在我的研究中,我必须编写一个 Android 应用程序来从气象站检索天气数据。这些将显示在块中。
这些块将分成 4 列和 2 行。
所以我想创建一个 4 列 2 行的方形网格来提供块。
有人可以提供解决方案来帮助我创建这个网格吗?
有很多选择:
1.您可以使用GridLayoutManager选择回收器视图
<android.support.v7.widget.RecyclerView
android:id="@+id/main_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp"
/>
所以在活动内部你会做类似的事情
mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), COLUMN_COUNT));
2.您选择网格视图
<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"/>
对于固定的项目尺寸,您应该使用
<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>
确保将其添加到应用程序 gradle 中
compile 'com.android.support:gridlayout-v7:23.0.1'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)