【问题标题】:Dynamic Tablerow - inserting a textview contents in an existing tablerow动态表格行 - 在现有表格行中插入文本视图内容
【发布时间】:2019-01-29 11:50:36
【问题描述】:

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

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

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;
}

ma​​in_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. mealRow(Tablerow)中新增TextView

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

【问题讨论】:

    标签: java android android-tablelayout tablerow


    【解决方案1】:

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

    这是因为 TextView 进入了 TableRow。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多