简体   繁体   English

如何使用 Java 设置 columnWeight

[英]How to set columnWeight with Java

The title explains what I need.标题解释了我需要什么。 I want to set columnWeight of Button (Because is inside GridLayout) but from code.我想设置 Button 的 columnWeight(因为在 GridLayout 内)但是从代码中设置。

Something like that with code:类似的代码:

<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" />

You need to use LinearLayout subviews.您需要使用 LinearLayout 子视图。 Like the user Price says:就像用户价格说的:

There are limitations when using the GridLayout, the following quote is taken from the documentation .使用 GridLayout 时有一些限制,以下引用来自文档

"GridLayout does not provide support for the principle of weight, as defined in weight. In general, it is not therefore possible to configure a GridLayout to distribute excess space in non-trivial proportions between multiple rows or columns ... For complete control over excess space distribution in a row or column; use a LinearLayout subview to hold the components in the associated cell group." “GridLayout 不支持权重原则,如权重所定义。一般来说,因此不可能配置 GridLayout 以在多行或多列之间以非平凡的比例分配多余的空间......为了完全控制行或列中的多余空间分布;使用 LinearLayout 子视图将组件保存在关联的单元格组中。”

Here is a small example that uses LinearLayout subviews.这是一个使用 LinearLayout 子视图的小例子。 (I used Space Views that takes up unused area and pushes the buttons into desired position.) (我使用了占据未使用区域并将按钮推到所需位置的空间视图。)

<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>

Check Price's response for more information.查看价格的回复以获取更多信息。

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

You can use spec() method to define the columnWeight from java code.您可以使用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