如何获得TextView的边距?

时间:2014-10-27 10:43:04

标签: java android

我试图获得textview的顶部和左边距,我给了硬编码的边距。

为什么我要这样做?因为我想循环浏览所有文本视图,所以我将这个文本视图置于这个文本视图之下,这样我就可以在这些精确的边缘上编程添加radiobutton组。

textView[0] = new TextView(context);
relativeLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
textView[0].setId(1); // changed id from 0 to 1
textView[0].setText(naam[0].toUpperCase());
relativeLayoutParams.setMargins(24, 39, 0, 0);
int tyo = relativeLayoutParams.getTop(); //Trying to get top margins.

2 个答案:

答案 0 :(得分:5)

嗯,你可以简单地反过来说。只需获取布局参数。

RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) textView.getLayoutParams();

然后你可以通过全局变量访问边距。

lp.topMargin
lp.leftMargin
lp.bottomMargin
lp.rightMargin

答案 1 :(得分:0)

试试这个

RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    rl.addRule(RelativeLayout.CENTER_VERTICAL);
    RelativeLayout.LayoutParams textParams;
    textParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                                                 LayoutParams.WRAP_CONTENT);

    textParams.addRule(RelativeLayout.RIGHT_OF, id_of_left_content);
    textParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, id_of_top_content);
    textParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, id_of_bottom_content);
    textParams.addRule(RelativeLayout.CENTER_VERTICAL);

我希望这个概念可以帮到你