从android中的数组创建动态按钮

时间:2016-10-24 17:32:06

标签: java android xml

我正在创建一个部分,我从数组中检索很多名称并以按钮的形式显示它们,例如Soundcloud如何显示趋势类型。有人能告诉我如何实现这一点或给我一些暗示吗?现在,我用2 for for循环做了它,但它没有正常工作.. Check the image

我的代码到现在为止

for (int i = 0; i < 3; i++) {
    LinearLayout row = new LinearLayout(this);
    row.setLayoutParams(new     LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

    for (int j = 0; j < 4; j++) {
        TextView btnTag = new Button(this);
        btnTag.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = size.x;
        int height = size.y;
        String someText = (web[(j + 1 + (i * 4))-1]) ;
        TextView myTextView = (TextView) findViewById(R.id.disid);
        float w = myTextView.getPaint().measureText(someText);
        tot = tot + w;
        btnTag.setBackgroundResource(R.drawable.buttonbg);

        Log.d("array", "CURRENT Activity ::"+ "final" + "  " + w+"  "+n+"  "+m);
        btnTag.setText(web[(j + 1 + (i * 4))-1]);
        btnTag.setId(j + 1 + (i * 4));
        row.addView(btnTag);
    }
    layout1.addView(row);
}

但是这只生成4个按钮和3行,我希望它是动态的,并决定从数组中获取的单词的包装。请帮帮我!

0 个答案:

没有答案