我无法在运行时以编程方式设置android:

时间:2013-12-29 11:56:02

标签: android xml layout

请以编程方式设置一个属性android:below。帮助我。

我的代码:

TextView title1 = new TextView(News.this);
title1.setText("Заголовок новости 1  -  2013.01.01\n\nСегодня состоится заседание зампредпал тра та та");
title1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT));
((ViewGroup)findViewById(R.id.newslayout)).addView(title1);
title1.setTextColor(getResources().getColor(R.color.colorWihte));
title1.setShadowLayer(2, 0, 0, getResources().getColor(R.color.colorBlack));
title1.setBackground(getResources().getDrawable(R.drawable.round_block));

RelativeLayout.LayoutParams layoutParams1 =(RelativeLayout.LayoutParams)title1.getLayoutParams();
layoutParams1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layoutParams1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
layoutParams1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
title1.setLayoutParams(layoutParams1);


TextView title2 = new TextView(News.this);
title2.setText("Заголовок новости 2");
title2.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT));
((ViewGroup)findViewById(R.id.newslayout)).addView(title2);
title2.setTextColor(getResources().getColor(R.color.colorWihte));
title2.setShadowLayer(2, 0, 0, getResources().getColor(R.color.colorBlack));

RelativeLayout.LayoutParams layoutParams2 = (RelativeLayout.LayoutParams)title2.getLayoutParams();
layoutParams2.addRule(RelativeLayout.BELOW, R.id.title1); // this line
layoutParams2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
layoutParams2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
title2.setLayoutParams(layoutParams2);

这一行:

layoutParams2.addRule(RelativeLayout.BELOW,R.id.title1);

1 个答案:

答案 0 :(得分:1)

您已动态创建TextView(未从任何布局中充气),并且您尚未将Id设置为TextView title1。将title1 id设置为R.id.title1

这可能对你有帮助......

    int textId1 = 0xFEED;
    int textId2 = 0xDEAF;
    // you can define above ID's as constants globally for future references...
    TextView title1 = new TextView(MainActivity.this);
    title1.setText("Заголовок новости 1  -  2013.01.01\n\nСегодня состоится заседание зампредпал тра та та");
    title1.setTextColor(getResources().getColor(R.color.colorWihte));
    title1.setShadowLayer(2, 0, 0,getResources().getColor(R.color.colorBlack));
    title1.setBackground(getResources().getDrawable(R.drawable.round_block));
    title1.setId(textId1);

    RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    layoutParams1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    layoutParams1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    layoutParams1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    title1.setLayoutParams(layoutParams1);
    ((ViewGroup) findViewById(R.id.newslayout)).addView(title1);

    TextView title2 = new TextView(MainActivity.this);
    title2.setText("Заголовок новости 2");
    title2.setTextColor(getResources().getColor(R.color.colorWihte));
    title2.setShadowLayer(2, 0, 0,getResources().getColor(R.color.colorBlack));
    title2.setId(textId2);

    RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    layoutParams2.addRule(RelativeLayout.BELOW, textId1);
    layoutParams2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    layoutParams2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    title2.setLayoutParams(layoutParams2);
    ((ViewGroup) findViewById(R.id.newslayout)).addView(title2);