即使尝试了所有可能,也无法动态添加表格布局

问题描述

我尝试了所有可能的LayoutParams在CardView中将表行添加到表布局。而且,奇怪的是我能够在同一视图的相对布局中添加textview。为什么表布局既没有在Tablelayout中添加textview,edittext也没有按钮?

java文件

package com.example.navigation_bar;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.linearlayoutmanager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.tabs.TabLayout;

import org.w3c.dom.Text;

import java.sql.BatchUpdateException;
import java.util.ArrayList;

public class schooltt extends Fragment implements View.OnClickListener
{
    View v,up_view;
    RelativeLayout rl;
    public int dynamic_id=1000;
    TextView tv;
    private  Fragment mMyFragment;
    View m_view;
    public Bundle savedState = null;
    private TextView vstup;
    public ArrayList<example_item>example_items;
    private RecyclerView recyclerView;
    private RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager mLayoutmanager;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_sctimetb,container,false);
        Button button = (Button) v.findViewById(R.id.btn_add);
        example_items = new ArrayList<>();
        View apdu = inflater.inflate(R.layout.example_cardview,false);
        TableLayout tableLayout = (TableLayout)apdu.findViewById(R.id.table_in_cardview);
        TableRow tableRow = new TableRow(apdu.getContext());
        Button b = new Button(apdu.getContext());
        b.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT));
        b.setText("Button");
        tableRow.addView(b);
        tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT));
        EditText t = new EditText(apdu.getContext());
        t.setText("HELLO WORLD");
        t.setTextColor(Color.BLACK);
        t.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT));
        tableRow.addView(t); // Problem may arise here.
        tableLayout.addView(tableRow); //Problem may arise here.
        example_items.add(new example_item("hello","world",tableLayout));
        example_items.add(new example_item("hello",tableLayout));

        recyclerView = (RecyclerView)v.findViewById(R.id.recyclerview);
        recyclerView.setHasFixedSize(true);
        mLayoutmanager = new linearlayoutmanager(this.getContext());
        adapter = new example_adapter(example_items);
        recyclerView.setLayoutManager(mLayoutmanager);
        recyclerView.setAdapter(adapter);
        tv = v.findViewById(R.id.tv);
        rl = v.findViewById(R.id.sctt);
        button.setonClickListener((View.OnClickListener) this);
       return v;
    }


   @Override
    public void onClick(View view) {

        switch (view.getId())
        {
            case R.id.btn_add:
                break;
        }
    }
}

XML文件

(1)example_cardview.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardCornerRadius="4dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="4dp"
        android:id="@+id/add_karvanu">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:textSize="20dp"
        android:textColor="#000000"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView2"
            android:textSize="20dp"
            android:layout_below="@+id/textView"
            android:textColor="#000000"/>

        <TableLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView2"
            android:id="@+id/table_in_cardview"/>

    </RelativeLayout>

</androidx.cardview.widget.CardView>

(2)fragment_sctimetb.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_blue_light"
    android:id="@+id/sctt">

    <RelativeLayout
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_centerHorizontal="true"
        android:id="@+id/ab">
        <Button
            android:id="@+id/btn_delete"
            android:layout_marginTop="0sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="DELETE"
            android:drawableRight="@drawable/delete_icon"
            android:layout_alignParentLeft="true"/>

        <Button
            android:id="@+id/btn_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/btn_delete"
            android:text="CREATE"
            android:drawableRight="@drawable/add_icon"
            />

        <Button
            android:id="@+id/btn_undo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableRight="@drawable/undo_icon"
            android:layout_toRightOf="@+id/btn_add"
            android:text="UNDO"
            />

        <Button
            android:id="@+id/btn_save"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/btn_undo"
            android:drawableRight="@drawable/save_icon"
            android:text="Save"
            />
    </RelativeLayout>
    <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/ab"
       android:textSize="30sp"
       android:id="@+id/tv"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:label="School TiMetable"
        android:text="School TiMetable"
        android:textSize="30sp" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="4dp"
        android:background="@android:color/darker_gray"
        android:layout_below="@+id/ab"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginRight="10sp"
        android:layout_marginBottom="10sp"
        android:gravity="bottom|end">

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            style="?android:attr/borderlessButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/roundedbutton"
            android:backgroundTint="#41237d"
            app:srcCompat="@drawable/ic_add_table" /> <!-- background color of button -->
    </RelativeLayout>
</RelativeLayout>

我尝试了所有情况。我认为问题出在向表布局添加表行或向表行添加textview,edittext,button的某个地方。我的案件怎么了?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...