Android:如何以编程方式创建GridLayout [而非GridView]? [英] Android: How to create a GridLayout [not GridView] programmatically?

查看:61
本文介绍了Android:如何以编程方式创建GridLayout [而非GridView]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发匹配以下内容"应用程序.该应用程序使用的xml如下:

I am developing a "match the following" application. The xml used for the application is as follows:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lytLinear"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txtQuestion"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="30dp"
        android:text="Match the following questions with their answers." />

    <GridLayout
        android:id="@+id/lytGrid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnCount="3"
        android:columnWidth="300dp"
        android:orientation="vertical"
        android:rowCount="5"
        android:stretchMode="columnWidth" >

        <LinearLayout
            android:id="@+id/Q1Lyt"
            android:layout_width="160dp"
            android:layout_height="50dp"
            android:layout_column="0"
            android:layout_row="0"
            android:background="@drawable/shape_qn"
            android:gravity="center" >

            <TextView
                android:id="@+id/Q1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_row="0"
                android:text="Question1" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/A1Lyt"
            android:layout_width="160dp"
            android:layout_height="50dp"
            android:layout_column="1"
            android:layout_row="0"
            android:background="@drawable/shape"
            android:gravity="center"
            android:text="Answer1" >

            <TextView
                android:id="@+id/A1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_row="0"
                android:text="Answer1" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/Q2Lyt"
            android:layout_width="160dp"
            android:layout_height="50dp"
            android:layout_column="0"
            android:layout_row="1"
            android:background="@drawable/shape_qn"
            android:gravity="center" >

            <TextView
                android:id="@+id/Q2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Question2" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/A2Lyt"
            android:layout_width="160dp"
            android:layout_height="50dp"
            android:layout_column="1"
            android:layout_row="1"
            android:background="@drawable/shape"
            android:gravity="center" >

            <TextView
                android:id="@+id/A2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_row="0"
                android:text="Answer2" />
        </LinearLayout>
   </GridLayout>
 </LinearLayout>

我尝试了以下代码来创建网格布局.但是,对于子级线性布局,我无法找到以编程方式设置android:layout_column和android:layout_row参数的方法.

I have tried the following code to create the grid layout. However, for the child linear layouts, I am unable to find a way to programmatically set the android:layout_column and android:layout_row parameters.

GridLayout gridLayout = new GridLayout(this);
    gridLayout.setColumnCount(2);
    gridLayout.setRowCount(15);

    LinearLayout lyt1 = new LinearLayout(this);
    LinearLayout.LayoutParams prmsLinLyt = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

有指针吗?

推荐答案

以下是您以编程方式创建它的方法:

Here is how You can create it programmatically:

GridLayout gridLayout = new GridLayout(getContext());

// Add Title
GridLayout.Spec titleTxtSpecColumn = GridLayout.spec(2, GridLayout.BASELINE);
GridLayout.Spec titleRowSpec = GridLayout.spec(0);
TextView titleText = new TextView(getContext());
titleText.setText("Title");

gridLayout.addView(titleText, new GridLayout.LayoutParams(titleRowSpec , titleTxtSpecColumn));

有关更多详细信息,请参见ApiDemos示例.

For more details see ApiDemos examples.

这篇关于Android:如何以编程方式创建GridLayout [而非GridView]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆