android - 在线性布局中使用 inflator 添加表格行

标签 android android-layout android-linearlayout layout-inflater

我正在尝试使用 LayoutInflater 将表格行添加到表格。

这是我的 XML 文件,名为 customlistviewitem.xml

   <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:id="@+id/LinearLayout01"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">

   <ScrollView
   android:id="@+id/ScrollView1"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:layout_marginTop="75dp">
  <TableLayout 
android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:orientation="vertical" 
android:id="@+id/myTableLayout1">

<TableRow android:layout_marginTop="1dp" android:layout_marginLeft="10dp">

    </TableRow>
    </TableLayout>

    </ScrollView>
    </LinearLayout>

这是我用来向表中添加行的代码

    LinearLayout l = (LinearLayout) findViewById(R.id.mylayout1);
            LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            JSONArray jArray;
            try {
                jArray = new JSONArray(result);

            JSONObject json_data=null;
            for(int i=0;i<jArray.length();i++){
                //Log.e("Carlist","Result" +result);
                json_data = jArray.getJSONObject(i);
                   int ct_id = json_data.getInt("id");
                   String make = json_data.getString("make");
                   String model = json_data.getString("model");
                   String price = json_data.getString("price");

                   TableRow tr = new TableRow(this);
                         tr.setId(ct_id);
                         tr.setClickable(true);

                         tr.setOnClickListener(new  OnClickListener() {

                                @Override
                                public void onClick(final View v) {

                                    String sdet_id;
                                    int det_id;
                        //          v.setBackgroundColor(Color.GRAY);
                                    det_id = v.getId();
                                    sdet_id = String.valueOf(det_id);
                                    final Intent i = new Intent();
                                    i.setClassName("demo.example.com", "demo.example.com.cardetails");
                                    i.putExtra("Det_id", sdet_id);
                                    startActivity(i);

                          //        v.setBackgroundColor(Color.TRANSPARENT);
                                    // TODO Auto-generated method stub
                                }
                            });
                         TableLayout.LayoutParams tableRowParams=
                              new TableLayout.LayoutParams
                              (TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);

                            int leftMargin=20;
                            int topMargin=10;
                            int rightMargin=15;
                            int bottomMargin=20;

                            tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);

                            tr.setLayoutParams(tableRowParams);

                              /* Create a Button to be the row-content. */
                         ImageView myimage = new ImageView(this);

                         BitmapFactory.Options bmOptions;
                        bmOptions = new BitmapFactory.Options();
                        bmOptions.inSampleSize = 1;
                        Bitmap bm = LoadImage(image_URL, bmOptions);
                        myimage.setImageBitmap(bm);
                         tr.addView(myimage);
                         TextView tmake=new TextView(this);
                        // tmake.setText(make);
                         tmake.setText(Html.fromHtml("<H1>" + make));
                         tr.addView(tmake); 

                         TextView tmodel=new TextView(this);
                         tmodel.setText(Html.fromHtml("<b><H1>" + "&nbsp;&nbsp;" + "€" + price + "</b></H1></br></br>" ));
                         tr.addView(tmodel);



                    /* Add row to TableLayout. */
                     //  tr.setPadding(50, 0, 0, 0);
                         tl.addView(tr);
                         View v = new View(this);
                         v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
                         v.setBackgroundColor(Color.rgb(51, 51, 51));
                         tl.addView(v);

            }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

这不是添加表格行。我需要做什么来添加行和文本项?

最佳答案

问题似乎是 LayoutParams 设置为 TableRow,View 被添加到 TableRow,尝试注释设置布局参数代码,然后检查结果。

LinearLayout l = (LinearLayout) findViewById(R.id.mylayout1);
            LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            JSONArray jArray;
            try {
                jArray = new JSONArray(result);

            JSONObject json_data=null;
            for(int i=0;i<jArray.length();i++){
                //Log.e("Carlist","Result" +result);
                json_data = jArray.getJSONObject(i);
                   int ct_id = json_data.getInt("id");
                   String make = json_data.getString("make");
                   String model = json_data.getString("model");
                   String price = json_data.getString("price");

                   TableRow tr = new TableRow(this);
                         tr.setId(ct_id);
                         tr.setClickable(true);

                         tr.setOnClickListener(new  OnClickListener() {

                                @Override
                                public void onClick(final View v) {

                                    String sdet_id;
                                    int det_id;
                        //          v.setBackgroundColor(Color.GRAY);
                                    det_id = v.getId();
                                    sdet_id = String.valueOf(det_id);
                                    final Intent i = new Intent();
                                    i.setClassName("demo.example.com", "demo.example.com.cardetails");
                                    i.putExtra("Det_id", sdet_id);
                                    startActivity(i);

                          //        v.setBackgroundColor(Color.TRANSPARENT);
                                    // TODO Auto-generated method stub
                                }
                            });


                              /* Create a Button to be the row-content. */
                         ImageView myimage = new ImageView(this);

                         BitmapFactory.Options bmOptions;
                        bmOptions = new BitmapFactory.Options();
                        bmOptions.inSampleSize = 1;
                        Bitmap bm = LoadImage(image_URL, bmOptions);
                        myimage.setImageBitmap(bm);
                         tr.addView(myimage);
                         TextView tmake=new TextView(this);
                        // tmake.setText(make);
                         tmake.setText(Html.fromHtml("<H1>" + make));
                         tr.addView(tmake); 

                         TextView tmodel=new TextView(this);
                         tmodel.setText(Html.fromHtml("<b><H1>" + "&nbsp;&nbsp;" + "€" + price + "</b></H1></br></br>" ));
                         tr.addView(tmodel);



                    /* Add row to TableLayout. */
                     //  tr.setPadding(50, 0, 0, 0);
                         tl.addView(tr);
                         View v = new View(this);

                         v.setBackgroundColor(Color.rgb(51, 51, 51));
                         tl.addView(v);

            }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

关于android - 在线性布局中使用 inflator 添加表格行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11738085/

相关文章:

android - 通过 sql 循环将多个 TextView 动态添加到 TableRow

android - 使用 getSharedPreferences 获取时间

android - 如何在android studio中编写平方根

java - Android-按钮滑动效果

android - RecyclerView涟漪效应不起作用

java - 向后兼容的 LinearLayout 构造函数

java - ViewPager nullPointerException 错误

android - 如何编写 Android 4.0 样式部分

android - 如何在 LinearLayout 中旋转 textView?

android - ionic + cordova 插件和后台模式