Android:如何以编程方式创建 GridLayout [不是 GridView]?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12280442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: How to create a GridLayout [not GridView] programmatically?
提问by Srikanth
I am developing a "match the following" application. The xml used for the application is as follows:
我正在开发一个“匹配以下”应用程序。应用程序使用的xml如下:
<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>
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.
我尝试了以下代码来创建网格布局。但是,对于子线性布局,我无法找到以编程方式设置 android:layout_column 和 android:layout_row 参数的方法。
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);
Any pointers?
任何指针?
回答by Roger Alien
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));
For more details see ApiDemos examples.
有关更多详细信息,请参阅 ApiDemos 示例。
回答by Aditya Nikhade
You should try this :
你应该试试这个:
gridLayout.setLayoutParams(prmsLinLyt);
and if you want to give values...
如果你想给出价值......
LinearLayout.LayoutParams prmsLinLyt = new LinearLayout.LayoutParams(90,100);
where layout_width = 90
and layout_height= 100