简体   繁体   中英

How programmatically set android:layout_row?

The question is above. This is the activity.xml with android:layout_row attribute:

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

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

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?

you can set them by LayoutParams like this :

grid.addView(subview, new GridLayout.LayoutParams(
                          GridLayout.spec(1, GridLayout.CENTER),
                          GridLayout.spec(1, GridLayout.CENTER)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM