如何在Java中将许多TextView添加到GridLayout?

时间:2019-04-22 21:21:16

标签: java android

我的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="2"
        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"
            />
    </GridLayout>

</android.support.constraint.ConstraintLayout>

如何在Java中将许多此类TextField添加到GridLayout中。 我尝试过:

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.setBackgroundResource(R.drawable.shape);
        textView.setGravity(Gravity.CENTER);
        textView.setText("Hallo");

        GridLayout grid= findViewById(R.id.gridLayout);
        grid.addView(textView);
        grid.addView(textView);
    }
}

但是,如果我加的更多,那么1 TextView就会出现错误: The specified child already has a parent. You must call removeView() on the child's parent first.我该怎么做?

1 个答案:

答案 0 :(得分:0)

您可以将自定义视图GridChild添加到GridLayout,其中可以包含多个TextViews

<?xml version="1.0" encoding="utf-8"?>

<GridLayout
    android:id="@+id/gridLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:rowCount="1"
    android:columnCount="2"
    android:useDefaultMargins="true">

    <com.example.customview.GridChild/>

</GridLayout>

和您的GridChild

package com.example.customview;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TextView;

public class GridChild extends LinearLayout {

    private TextView text1;
    private TextView text2;

    public MyView(Context context) {
        super(context);
        init();
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        inflate(getContext(), R.layout.layout_ll, this);
        this.text1 = (TextView)findViewById(R.id.textview1);
        this.text2 = (TextView)findViewById(R.id.textview2);
    }
}