在RelativeLayout的子视图设置参数 [英] Setting parameters on child views of a RelativeLayout

查看:170
本文介绍了在RelativeLayout的子视图设置参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到正是我需要使用设置paramters上的相对布局子视图的语法。我有我要设置2子textviews相邻的这样一个根相对布局

---------- ---------
|其次| |第一页|
---------- ---------

所以,我有

 公共类RL扩展RelativeLayout的{

    公共RL(上下文){

        TextView的第一=新的TextView(本);
        TextView的第二=新的TextView(本);

        first.setText(第一);
        first.setId(1);

        second.setText(二);
        second.setId(2);

        addView(第一,新RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
    LayoutParams.WRAP_CONTENT,
    LayoutParams.ALLIGN_PARENT_RIGHT ???);

        addView(第一,新RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
    LayoutParams.WRAP_CONTENT,
    LayoutParams.ALLIGN_RIGHT_OF(first.getId())???);

    }
}
 

我如何设置相对比对?

解决方案

 公共类RL扩展RelativeLayout的{

    公共RL(上下文的背景下){
        超(上下文);

        TextView的第一=新的TextView(上下文);
        TextView的第二=新的TextView(上下文);

        first.setText(第一);
        first.setId(1);

        second.setText(二);
        second.setId(2);

        RelativeLayout.LayoutParams lpSecond =新RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        addView(第二,lpSecond);

        RelativeLayout.LayoutParams lpFirst =新RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        lpFirst.addRule(RelativeLayout.RIGHT_OF,second.getId());
        addView(第一,lpFirst);
    }

}
 

您只需要ALIGN_PARENT_RIGHT如果你想查看的右边缘排队与其父的右边缘。在这种情况下,就成了推第一个视图关闭的可见区域的一侧<!/ P>

I'm having trouble finding exactly the syntax I need to use to set the paramters on child views of a relative layout. I have a root relative layout that I want to set 2 child textviews next to each other like this

---------- ---------
| Second | | First |
---------- ---------

So I have

public class RL extends RelativeLayout{

    public RL(context){

        TextView first = new TextView(this);
        TextView second = new TextView(this);

        first.setText('First');
        first.setId(1);

        second.setText('Second');
        second.setId(2);

        addView(first, new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
    LayoutParams.WRAP_CONTENT, 
    LayoutParams.ALLIGN_PARENT_RIGHT ???);

        addView(first, new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
    LayoutParams.WRAP_CONTENT, 
    LayoutParams.ALLIGN_RIGHT_OF(first.getId()) ???);

    }
}

How do I set the relative alignments?

解决方案

public class RL extends RelativeLayout {

    public RL(Context context) {
        super(context);

        TextView first = new TextView(context);
        TextView second = new TextView(context);

        first.setText("First");
        first.setId(1);

        second.setText("Second");
        second.setId(2);

        RelativeLayout.LayoutParams lpSecond = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        addView(second, lpSecond);

        RelativeLayout.LayoutParams lpFirst = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lpFirst.addRule(RelativeLayout.RIGHT_OF, second.getId());
        addView(first, lpFirst);
    }

}

You only need ALIGN_PARENT_RIGHT if you want the right edge of the view to line up with the right edge of its parent. In this case, it would push the 'first' view off the side of the visible area!

这篇关于在RelativeLayout的子视图设置参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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