如何为GridLayout的嵌套元素指定权重和宽度

时间:2016-10-14 16:09:26

标签: android android-layout

我有两列android.support.v7.widget.GridLayout。我想向网格添加字段,但不知道如何为此指定属性。

我的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp"
    tools:context="com.mappolygon.PolygonDialogFragment">

    <android.support.v7.widget.GridLayout
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        app:columnCount="2">

        <android.support.design.widget.TextInputLayout
            android:id="@+id/layout_lat1"
            android:layout_width="0dp"
            app:layout_columnWeight="1">

            <EditText
                android:id="@+id/et_lat1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/latitude_1"
                android:inputType="numberDecimal|numberSigned" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/layout_long1"
            android:layout_width="0dp"
            app:layout_columnWeight="1">

            <EditText
                android:id="@+id/et_long1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/longitude_1"
                android:inputType="numberDecimal|numberSigned" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/layout_lat2"
            android:layout_width="0dp"
            app:layout_columnWeight="1">

            <EditText

                android:id="@+id/et_lat2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/latitude_2"
                android:inputType="numberDecimal|numberSigned" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/layout_long2"
            android:layout_width="0dp"
            app:layout_columnWeight="1">

            <EditText
                android:id="@+id/et_long2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/longitude_2"
                android:inputType="numberDecimal|numberSigned" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/layout_lat3"
            android:layout_width="0dp"
            app:layout_columnWeight="1">

            <EditText
                android:id="@+id/et_lat3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/latitude_3"
                android:inputType="numberDecimal|numberSigned" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/layout_long3"
            android:layout_width="0dp"
            app:layout_columnWeight="1">

            <EditText
                android:id="@+id/et_long3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/longitude_3"
                android:inputType="numberDecimal|numberSigned" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/layout_lat4"
            android:layout_width="0dp"
            app:layout_columnWeight="1">

            <EditText
                android:id="@+id/et_lat4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/latitude_4"
                android:inputType="numberDecimal|numberSigned" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/layout_long4"
            android:layout_width="0dp"
            app:layout_columnWeight="1">

            <EditText
                android:id="@+id/et_long4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/longitude_4"
                android:inputType="numberDecimal|numberSigned" />
        </android.support.design.widget.TextInputLayout>
    </android.support.v7.widget.GridLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_add_coordinates"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/ic_add" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_build_polygon"
            style="@style/Base.Widget.AppCompat.Button.Colored"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:text="@string/build_polygon" />

        <Button
            android:id="@+id/btn_cancel"
            style="@style/Base.Widget.AppCompat.Button.Colored"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/cancel" />
    </LinearLayout>
</LinearLayout>

我的按钮处理(我正在使用Butterknife

   @Bind({R.id.et_lat1, R.id.et_long1, R.id.et_lat2, R.id.et_long2, R.id.et_lat3, R.id.et_long3, R.id.et_lat4, R.id.et_long4})
   List<EditText> coordinatesList;
   @Bind({R.id.layout_lat1, R.id.layout_long1, R.id.layout_lat2, R.id.layout_long2, R.id.layout_lat3, R.id.layout_long3, R.id.layout_lat4, R.id.layout_long4})
   List<TextInputLayout> inputLayoutList;
   @Bind(R.id.grid)
   GridLayout gridLayout;
**...**

   @OnClick(R.id.fab_add_coordinates)
   void addFields(){
        TextInputLayout inputLayoutLat = new TextInputLayout(getActivity());
        TextInputLayout inputLayoutLng = new TextInputLayout(getActivity());
        EditText editTextLat = new EditText(getActivity());
        EditText editTextLng = new EditText(getActivity());

        inputLayoutLat.addView(editTextLat);
        inputLayoutLng.addView(editTextLng);

        coordinatesList.add(editTextLat);
        coordinatesList.add(editTextLng);
        inputLayoutList.add(inputLayoutLat);
        inputLayoutList.add(inputLayoutLng);

        gridLayout.addView(inputLayoutLat);
        gridLayout.addView(inputLayoutLng);
    }

那么,在这种情况下如何设置重量和宽度?

1 个答案:

答案 0 :(得分:0)

当您希望某些视图跨越多行/列时,

GridLayout特别适合将视图放入网格中。通常在这些情况下,您知道某个视图需要在哪一行/列中。

仔细观察您的布局,我发现您正在编辑纬度/经度值,因此您可能需要两列。我猜你想要以编程方式添加行,以便用户输入任意数量的坐标对。

如果是这种情况,我建议您使用ListView查看视图,并在每次需要新坐标对时向列表中添加新项。您的列表项视图可以是简单的水平LinearLayout。然后,您可以实现在具有任意行的同时加权视图宽度的目标。

是的,您可以使用GridLayout&#39; s LayoutParams在代码中设置列权重。

     int row = ...;
     int col = ...;
     float rowWeight = ...;
     float colWeight = ...;
     GridLayout.Spec rowSpec = GridLayout.spec(row, rowWeight);
     GridLayout.Spec colSpec = GridLayout.spec(col, colWeight);
     GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpec, colSpec);
     params.width = ...;
     params.height = LayoutParams.WRAP_CONTENT;
     inputLayoutLat.setLayoutParams(params);

如前所述,这仅适用于API 21和转发。