Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

How do I specify width and height of Android GridLayout cells in relative measures?

I'm trying to figure out how GridLayout works, but one thing I can't figure out from the documentation is how or if one can control the size of the grid cells.

Say I want a two by two grid where each cell occupy exactly 25% of the screen real estate (half height, half width) - can I do this?

With LinearLayout I would accomplish this by nesting two horizontal LinearLayout's in one vertical and then assigning a weight of 1 to all elements. GridLayout does not support the weight property though.

question from:https://stackoverflow.com/questions/14500677/how-do-i-specify-width-and-height-of-android-gridlayout-cells-in-relative-measur

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There's two properties android:layout_columnWeight and layout_rowWeight which works like layout_weight in LinearLayout. This is supported in API 21. For older android devices, use the GridLayout from the v7 support library.

Those properties will allow you to adjust the width/height of column base on the value you assigned to each column. The formula goes like this (column_weight/ sum_of_column_weight) * gridLayout_width = column_width.

Here's an example which is a gridview with 2 rows and 2 columns evenly spread out, each takes 50% of the weight and height of the grid.

<GridLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="2"
>

    <TextView
        android:text="SEARCH FEE"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
     />

    <TextView
        android:layout_columnWeight="1"
        android:text="SEARCH FEE"
        android:layout_rowWeight="1"
     />

    <TextView
        android:text="SEARCH FEE"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        />

    <TextView
        android:layout_columnWeight="1"
        android:text="SEARCH FEE"
        android:layout_rowWeight="1"
        />
</GridLayout>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...