Как добавить много текстовых представлений в GridLayout на Java?

#java #android

#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>
  

Как я могу добавить многие из этих текстовых полей в GridLayout на 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.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:

Вы можете добавить пользовательский вид 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);
    }
}
  

Комментарии:

1. Во-первых, у меня есть Gridlayout , а не Gridview а во-вторых, как я могу сделать это в коде Java?

2. Спасибо. Я обновил свой вопрос. Как я могу добавить 4 TextViews I в 2×2 Gridlayout ?