动态表格行-在现有表格行中插入textview内容

时间:2019-01-29 11:50:37

标签: java android android-tablelayout tablerow

我正在创建我的第一个android应用程序,并想在我的应用程序中使用Dynamic Tablerow。在我的MainFragment.java中,有OnClickListener用于添加文本。我使用Log.d检查它是否正常工作。我检查了它的工作原理。但是没有在Tablerow中添加文本;其ID为mealRow。 我想在现有TextView中添加Tablerow。请检查并回答我。谢谢。

我尝试了新的Tablerow。但是它也失败了。 我的意思是在现有TextView的新Tablerow中的新TableLayout

MainFragment.java

public static MainFragment newInstance() {
    return new MainFragment();
}

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    final View view = inflater.inflate(R.layout.main_fragment, container, false);

    Response.Listener<String> responseListener;
    responseListener = new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            String mealList = response;
            Log.d(TAG, mealList+"");
            //TextView meal = (TextView) getView().findViewById(R.id.meal1);
            //meal.setText(mealList);
        }
    };
    Log.d(TAG, "Meal queued!");
    MealRequest mealRequest = new MealRequest(countryCode, schulCode, insttNm, schulCrseScCode,
            schMmealScCode, schYmd, responseListener);
    RequestQueue queue = Volley.newRequestQueue(getActivity());
    queue.add(mealRequest);

    Button create = (Button) view.findViewById(R.id.Create);

    create.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG, "button Clicked!");
            TableRow mealRow = (TableRow) view.findViewById(R.id.mealRow);
            ViewGroup.LayoutParams textParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            TextView meals = new TextView(getActivity());
            meals.setText("new");
            meals.setLayoutParams(textParams);
            meals.setGravity(Gravity.CENTER);
            meals.setTextColor(Color.parseColor("#000000"));
            meals.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
            mealRow.addView(meals);
        }
    });

    return view;
}

MainActivity.java

private final static String TAG = MainActivity.class.getName();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.add(R.id.fragment_container, MainFragment.newInstance()).commit();
    navigationView.setNavigationItemSelectedListener(this);

}

**--------------------------------cut--------------------------**

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_home) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, MainFragment.newInstance()).commit();
    } else if (id == R.id.nav_calendar) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, Calendar.newInstance()).commit();
    } else if (id == R.id.nav_timetable) {

    } else if (id == R.id.nav_homework) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

main_fragment.xml

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Today's meal"
    android:textColor="#000000"
    android:textSize="20sp"/>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TableLayout
        android:id="@+id/mealTable"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:stretchColumns="*">

        <TableRow
            android:id="@+id/mealRow"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/meal1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="1dp"
                android:layout_marginBottom="1dp"
                android:gravity="center"
                android:text="1"
                android:textColor="#000000"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/meal2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="1dp"
                android:layout_marginBottom="1dp"
                android:gravity="center"
                android:text="2"
                android:textColor="#000000"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/meal3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="1dp"
                android:layout_marginBottom="1dp"
                android:gravity="center"
                android:text="3"
                android:textColor="#000000"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/meal4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="1dp"
                android:layout_marginBottom="1dp"
                android:gravity="center"
                android:text="4"
                android:textColor="#000000"
                android:textSize="20sp" />

        </TableRow>
    </TableLayout>
</RelativeLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:orientation="horizontal">

    <Button
        android:id="@+id/Create"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Make Text!" />
</LinearLayout>

这是我的期望:

  1. 单击“创建”按钮

  2. 新的TextView已添加到餐行(Tablerow)中

但是实际输出是“什么都没有发生”。

1 个答案:

答案 0 :(得分:0)

您应该使用TableRow.LayoutParams而不是ViewGroup.LayoutParams。

这是因为TextView进入了TableRow。

如果TextView进入RelativeLayout,则应使用RelativeLayout.LayoutParams。