问题
i am trying to use GridView to divide the screen into six equal pieces. The problem is that it displays properly in the emulator but when i view it from the phone then the space is distributed unevenly. What could cause this behavior?
I have tested this with API 21 & API 22 phones.
Emulator vs Phone:
This is my xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.myapp.myapp.myapp.MainActivity"
tools:showIn="@layout/activity_main">
<GridLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:columnCount="2"
android:rowCount="3"
android:layout_centerInParent="false">
<Button
android:layout_column="0"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_columnWeight="1.0"
android:layout_rowWeight="1.0"
android:text="c0 | r0" />
<Button
android:layout_column="1"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_columnWeight="1.0"
android:layout_rowWeight="1.0"
android:text="c1 | r0" />
<Button
android:layout_column="0"
android:layout_gravity="fill"
android:layout_row="1"
android:layout_columnWeight="1.0"
android:layout_rowWeight="1.0"
android:text="c0 | r1" />
<Button
android:layout_column="1"
android:layout_gravity="fill"
android:layout_row="1"
android:layout_columnWeight="1.0"
android:layout_rowWeight="1.0"
android:layout_width="0dp"
android:text="c1 | r1" />
<Button
android:layout_column="0"
android:layout_gravity="fill"
android:layout_row="2"
android:layout_columnWeight="1.0"
android:layout_rowWeight="1.0"
android:text="c0 | r2" />
<Button
android:layout_column="1"
android:layout_gravity="fill"
android:layout_row="2"
android:layout_columnWeight="1.0"
android:layout_rowWeight="1.0"
android:text="c1 | r2" />
</GridLayout>
</RelativeLayout>
回答1:
As far as I know, the layout weight works only with LinearLayout
but you are using RelativeLayout
. Try to switch it.
来源:https://stackoverflow.com/questions/36977058/gridlayout-does-not-expand-elements-evenly