如何解决即使尝试了所有可能,也无法动态添加表格布局
我尝试了所有可能的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 举报,一经查实,本站将立刻删除。