以编程方式将textView旁边的另一个对齐

时间:2013-10-09 11:12:46

标签: android-layout textview android-ui android

我正在以编程方式创建文本视图,并尝试将另一个textView放在第一个文本视图旁边。但我无法做到。

这是我写的代码,

    //1st textview
     TextView itemText = new TextView(context);
    itemText.setText(mItemText);
    Typeface itemFont = Typeface.createFromAsset(context.getAssets(), "fonts/" + "Roboto" + ".ttf");
    itemText.setTypeface(itemFont,Typeface.BOLD);
    itemText.setPadding(0, padding, 0, 0);
    itemText.setId(10);
    RelativeLayout.LayoutParams itemTextParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);
    itemTextParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    itemTextParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    itemText.setTextSize(font_size);
    itemText.setLayoutParams(itemTextParams);

    //2nd text view
    TextView seperator = new TextView(context);
    seperator.setText(mSeperator);
    seperator.setPadding(0,padding,0,0);
    seperator.setTypeface(null,Typeface.BOLD);
    RelativeLayout.LayoutParams seperatorParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    seperatorParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    seperatorParams.addRule(RelativeLayout.RIGHT_OF,itemText.getId());
    seperatorParams.addRule(RelativeLayout.CENTER_VERTICAL);
    seperator.setLayoutParams(seperatorParams);

seperatorParams.addRule(RelativeLayout.CENTER_HORIZONTAL)有效,但是当我使用seperatorParams.addRule(RelativeLayout.RIGHT_OF,itemText.getId())时,文本不会显示。

有谁可以指出我哪里出错了?或者还有其他方法吗?

1 个答案:

答案 0 :(得分:0)

itemTextParams的宽度设置为MATCH_PARENT,因此无法在其右侧放置任何内容。将其更改为WRAP_CONTENT或定义宽度值。