[英]How programmatically set android:layout_row?
The question is above. 问题在于上面。 This is the
activity.xml
with android:layout_row
attribute: 这是带有
android:layout_row
属性的activity.xml
:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
android:id="@+id/mainWindow"
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="match_parent"
tools:context=".MainActivity"
>
<GridLayout
android:id="@+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="1"
android:columnCount="2"
android:useDefaultMargins="true"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_row="0"
android:layout_column="0"
android:background="@drawable/shape"
android:gravity="center"
android:text="Hallo"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_row="0"
android:layout_column="1"
android:background="@drawable/shape"
android:gravity="center"
android:text="Hallo"
/>
</GridLayout>
</android.support.constraint.ConstraintLayout>
Now my new activity.xml
file is: 现在我的新
activity.xml
文件是:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
android:id="@+id/mainWindow"
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="match_parent"
tools:context=".MainActivity"
>
<GridLayout
android:id="@+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="1"
android:columnCount="2"
android:useDefaultMargins="true"
>
</GridLayout>
</android.support.constraint.ConstraintLayout>
and my MainActivity.java
is: 和我的
MainActivity.java
是:
package com.example.sudokusolver;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.GridLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridLayout.LayoutParams lp= new GridLayout.LayoutParams();
lp.width= GridLayout.LayoutParams.MATCH_PARENT;
lp.height= GridLayout.LayoutParams.MATCH_PARENT;
lp.columnSpec= GridLayout.spec(GridLayout.UNDEFINED, 1f);
lp.rowSpec= GridLayout.spec(GridLayout.UNDEFINED, 1f);
TextView textView= new TextView(this);
textView.setLayoutParams(lp);
textView.setBackgroundResource(R.drawable.shape);
textView.setGravity(Gravity.CENTER);
textView.setText("Hallo");
TextView textView1= new TextView(this);
textView1.setLayoutParams(lp);
textView1.setBackgroundResource(R.drawable.shape);
textView1.setGravity(Gravity.CENTER);
textView1.setText("Hallo");
GridLayout grid= findViewById(R.id.gridLayout);
grid.addView(textView, 0);
grid.addView(textView1, 1);
}
}
So how can i set set android:layout_row
and android:layout_column=
parameters in Java? 那么如何在Java中设置
android:layout_row
和android:layout_column=
参数?
you can set them by LayoutParams
like this : 您可以通过
LayoutParams
设置它们,如下所示:
grid.addView(subview, new GridLayout.LayoutParams(
GridLayout.spec(1, GridLayout.CENTER),
GridLayout.spec(1, GridLayout.CENTER)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.