android - 在android中将网格布局列设置为宽度的一半

标签 android layout grid-layout

我有一个网格布局,在两个不同的列中有两个图像,图像没有填满所有可用空间。

我需要两列的宽度都是一半(至于现在最左边的列比另一列大)。我该怎么做?

这是我目前所拥有的,但 blackBowl 占用了大部分空间......

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res/com.whitepebbles"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity2" >

<android.support.v7.widget.GridLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    app:columnCount="4" >

    <TextView
        android:id="@+id/whiteBowl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_column="0"
        app:layout_gravity="center|top"
        app:layout_row="5" 
        android:background="@drawable/wbowl00"
        app:layout_columnSpan="2"/>

    <TextView
        android:id="@+id/blackBowl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_column="3"
        app:layout_columnSpan="2"
        app:layout_gravity="center"
        app:layout_row="5"
        android:background="@drawable/bbowl00" />

</android.support.v7.widget.GridLayout>

最佳答案

我想我用支持库 v7 修订版 21.0.2 解决了这个问题(我用 3 个 TextView 测试过):

<android.support.v7.widget.GridLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:columnCount="3"
        >
        <TextView
            app:layout_columnWeight="1"
            app:layout_gravity="fill"
            android:text="1"
            />
        <TextView
            app:layout_columnWeight="1"
            app:layout_gravity="fill"
            android:text="2"
            />
        <TextView
            app:layout_columnWeight="1"
            app:layout_gravity="fill"
            android:text="3"
            />
</android.support.v7.widget.GridLayout>

上面的代码生成了一行,其中包含三个 TextView,使用了 1/3 的可用空间。

关于android - 在android中将网格布局列设置为宽度的一半,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18670484/

相关文章:

android - GridLayout android.view.ViewGroup$LayoutParams 无法转换为 android.support.v7.widget.GridLayout$LayoutParams

android - 为什么定位应用程序崩溃

android - 即使多次单击按钮,如何只增加一次分数?

java - 如何在执行操作后移动按钮?

flutter - 如何在Flutter中创建可滚动的网格?

css - 如何在 wordpress 中创建具有多个布局的多个页面

android - Eclipse 报错 android.support.v7.widget.GridLayout 实例化失败

java - 如何停止使用 prepare() 和 startService() 调用的 VpnService?

java - 通过识别标签从 HTML 中获取信息

php - 您可以使用一个产品布局页面来显示点击后的产品吗?