【问题标题】:creating dynamic table in android在android中创建动态表
【发布时间】:2011-06-28 03:48:41
【问题描述】:

我正在尝试从一个二维字符串数组创建一个表。但是,我很喜欢在代码中创建视图,所以由于某种原因,我的图像永远不可见,而且我的文本视图不会分成多行,而是会截断文本。

此外,我在代码中的每个 textview 上都添加了 id,当有人单击 textview 时,我希望我可以使用该 ID 来识别单击了哪一行。不幸的是,事实并非如此。

有什么好的方法来识别正确的行吗?

问候

      for (int current=0; current<cityArr.length; current++) {

      TableRow tr = new TableRow(this);
      tr.setId(100+current);
      tr.setLayoutParams(new LayoutParams(
              LayoutParams.FILL_PARENT,
              LayoutParams.WRAP_CONTENT));   

      TextView labelTV = new TextView(this);
      labelTV.setId(current);
      labelTV.setText(cityArr[current][0]);
      labelTV.setTextColor(Color.BLACK);
      labelTV.setLayoutParams(new LayoutParams(
              LayoutParams.FILL_PARENT,
              LayoutParams.WRAP_CONTENT));
      tr.addView(labelTV);

      ImageView mapView = new ImageView(this);
      mapView.setImageResource(R.drawable.list_icon_map);
      mapView.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT, 5));
      tr.addView(mapView);

      tlcity.addView(tr, new TableLayout.LayoutParams(
              LayoutParams.FILL_PARENT,
              LayoutParams.WRAP_CONTENT));
  }

【问题讨论】:

    标签: android tablelayout tablerow layoutparams


    【解决方案1】:
    public class MainActivity extends Activity  {
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            ScrollView sv = new ScrollView(this);
    
            TableLayout ll=new TableLayout(this);
            HorizontalScrollView hsv = new HorizontalScrollView(this);
    
            for(int i=1;i<30;i++) {
                TableRow tbrow=new TableRow(this);
    
                for(int j=1;j<=20;j++) {
                    TextView tv1=new TextView(this);
                    String s1 = Integer.toString(i);
                    String s2 = Integer.toString(j);
                    String s3 = s1+s2;
                    int id = Integer.parseInt(s3);
                    tv1.setId(id);
    
                    tv1.setText("Dynamic TextView  no:     "+id);
                    tbrow.addView(tv1);
                }
                ll.addView(tbrow);
            }
            hsv.addView(ll);
            sv.addView(hsv);
            setContentView(sv);
        }
    }
    

    【讨论】:

      【解决方案2】:

      哈,结果证明 setID() 函数可以正常工作。只要我把 OnClickListener 放在正确的组件上...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-17
        • 2013-02-03
        • 2014-03-30
        • 1970-01-01
        • 2012-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多