簡體   English   中英

如何使用 Java 設置 columnWeight

[英]How to set columnWeight with Java

標題解釋了我需要什么。 我想設置 Button 的 columnWeight(因為在 GridLayout 內)但是從代碼中設置。

類似的代碼:

<Button android:layout_column="0"
    android:layout_row="0"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_rowWeight="1"
    android:layout_columnWeight="1" />

您需要使用 LinearLayout 子視圖。 就像用戶價格說的:

使用 GridLayout 時有一些限制,以下引用來自文檔

“GridLayout 不支持權重原則,如權重所定義。一般來說,因此不可能配置 GridLayout 以在多行或多列之間以非平凡的比例分配多余的空間......為了完全控制行或列中的多余空間分布;使用 LinearLayout 子視圖將組件保存在關聯的單元格組中。”

這是一個使用 LinearLayout 子視圖的小例子。 (我使用了占據未使用區域並將按鈕推到所需位置的空間視圖。)

<GridLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnCount="1"
    >
        <TextView
            android:text="2x2 button grid"
            android:textSize="32dip"
            android:layout_gravity="center_horizontal" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Space
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1" />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button 1" />
            <Space
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1" />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="start"
                android:text="Button 2" />
            <Space
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
        >
            <Space
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1" />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button 3" />
            <Space
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1" />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="start"
                android:text="Button 4" />
            <Space
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1" />
        </LinearLayout>
    </GridLayout>

查看價格的回復以獲取更多信息。

試試看: ((GridLayout.LayoutParams) button.getLayoutParams()).columnSpec = GridLayout.spec(GridLayout.UNDEFINED, 1f) ;

您可以使用spec()方法從 Java 代碼中定義columnWeight

GridLayout.LayoutParams param = new LayoutParams(GridLayout.spec(GridLayout.UNDEFINED, 1f),  GridLayout.spec(GridLayout.UNDEFINED, 1f));

button.setLayoutParams(param);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM