添加textviews阵列动态线性布局 [英] Adding array of textviews dynamically to linear layout

查看:140
本文介绍了添加textviews阵列动态线性布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试添加我定义为公共变量textviews阵列,但是当我运行应用程序,它的力量,只要它进入for循环关闭。这是code:

 的LinearLayout myLayout =(的LinearLayout)findViewById(R.id.my_layout);
            的LayoutParams LP =新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            对=新的TextView [num_match + 1];
            对于(int类型l = 1; L< = num_match,L ++)
            {
                对[L] .setTextSize(15);
                对[L] .setLayoutParams(LP);
                对[L] .setId(升);
                对[L] .setText(M1 [L] + - + M2 [L]);
                myLayout.addView(对[L]);
            }


解决方案

您需要创建新的TextViews投入TextView的数组,你跳过第一个索引(对[0] ),这会导致以后的麻烦:

 对=新的TextView [num_match]
对于(int类型l = 0; L< num_match,L ++)
{
    对[1] =新的TextView(本);
    对[L] .setTextSize(15);
    对[L] .setLayoutParams(LP);
    对[L] .setId(升);
    对[L] .setText(M1 [1 + 1] + - + M2 [1 + 1]);
    myLayout.addView(对[L]);
}


从您的意见,包括我这个简单的工作示例,以帮助您:

 公共类示例扩展活动{
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        的LinearLayout myLayout =(的LinearLayout)findViewById(R.id.my_layout);
        的LayoutParams LP =新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        的TextView []成对=新的TextView [4];
        为(int类型l = 0; L&4;:L ++)
        {
            对[1] =新的TextView(本);
            对[L] .setTextSize(15);
            对[L] .setLayoutParams(LP);
            对[L] .setId(升);
            对[L] .setText((L + 1)+:东西);
            myLayout.addView(对[L]);
        }    }
}

通过布局的main.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / my_layout
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直/>

I tried to add an array of textviews which i defined as a public variable, but when i run the application, it force closes as soon as it gets into for loop. This is the code:

LinearLayout myLayout = (LinearLayout) findViewById(R.id.my_layout);
            LayoutParams lp = new LayoutParams( LayoutParams.WRAP_CONTENT,    LayoutParams.WRAP_CONTENT);
            pairs=new TextView[num_match+1];
            for(int l=1;l<=num_match;l++)
            {
                pairs[l].setTextSize(15);
                pairs[l].setLayoutParams(lp);
                pairs[l].setId(l);
                pairs[l].setText(m1[l]+" - "+m2[l]);
                myLayout.addView(pairs[l]);
            }

解决方案

You need to create new TextViews to put into the TextView array and you are skipping the first index (pairs[0]) which will lead to trouble later:

pairs=new TextView[num_match];
for(int l=0; l<num_match; l++)
{
    pairs[l] = new TextView(this);
    pairs[l].setTextSize(15);
    pairs[l].setLayoutParams(lp);
    pairs[l].setId(l);
    pairs[l].setText(m1[l + 1]+" - "+m2[l + 1]);
    myLayout.addView(pairs[l]);
}


From your comments, I included this simple working example to help you:

public class Example extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LinearLayout myLayout = (LinearLayout) findViewById(R.id.my_layout);
        LayoutParams lp = new LayoutParams( LayoutParams.WRAP_CONTENT,    LayoutParams.WRAP_CONTENT);
        TextView[] pairs=new TextView[4];
        for(int l=0; l<4; l++)
        {
            pairs[l] = new TextView(this);
            pairs[l].setTextSize(15);
            pairs[l].setLayoutParams(lp);
            pairs[l].setId(l);
            pairs[l].setText((l + 1) + ": something");
            myLayout.addView(pairs[l]);
        }

    }
}

With the layout main.xml:

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

这篇关于添加textviews阵列动态线性布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆