【发布时间】:2017-04-06 03:01:15
【问题描述】:
我尝试配置 2 行 2 列的网格布局。但是这里的每个元素都占据了屏幕的整个宽度,而第二列在屏幕之外。请建议我在这里做错了什么。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="match_parent"
android:background="@color/colorTheme"
android:orientation="vertical">
<GridLayout
android:id="@+id/moodsGridLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:columnCount="2"
android:orientation="vertical"
android:rowCount="2">
<ImageButton
android:id="@+id/imageButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="0"
app:srcCompat="@mipmap/brewtv_logos" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="0"
app:srcCompat="@mipmap/brewtv_logos" />
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="1"
app:srcCompat="@mipmap/brewtv_logos" />
<ImageButton
android:id="@+id/imageButton4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="1"
app:srcCompat="@mipmap/brewtv_logos" />
</GridLayout>
</LinearLayout>
【问题讨论】:
-
对我来说似乎很明显,如果一列的宽度设置为
match_parent,其他列将没有空间。尝试为您的 ImageButtons 使用离散 dp 值。 -
由于元素被包围在GridLayout中,并且每个底层元素的行跨度仍然是1,所以它应该只占据屏幕的一半,对吧?如果我错了,请纠正我。
-
试试看。也许我错了。
-
我已经动态尝试过,结果符合预期。但是布局 xmls 造成了一些麻烦。
-
然后保留动态生成的并将xml值设置为
0dp或wrap_content
标签: android android-layout grid-layout