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

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

问题描述

我无法准确找到在相对布局的子视图上设置参数所需的语法.我有一个根相对布局,我想像这样设置 2 个相邻的子文本视图

<前>---------- ---------|第二 ||第一 |---------- ---------

所以我有

公共类RL扩展RelativeLayout{公共 RL(上下文){TextView first = new TextView(this);TextView second = new TextView(this);first.setText('第一个');first.setId(1);第二个.setText('第二个');第二个.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()) ???);}}

如何设置相对对齐?

解决方案

public class RL extends RelativeLayout {公共 RL(上下文上下文){超级(上下文);TextView first = new TextView(context);TextView second = new TextView(context);first.setText("第一个");first.setId(1);second.setText("第二");第二个.setId(2);RelativeLayout.LayoutParams lpSecond = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);添加视图(第二,lpSecond);RelativeLayout.LayoutParams lpFirst = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);lpFirst.addRule(RelativeLayout.RIGHT_OF, second.getId());addView(first, lpFirst);}}

如果您希望视图的右边缘与其父级的右边缘对齐,则只需要 ALIGN_PARENT_RIGHT.在这种情况下,它会将第一个"视图推离可见区域的一侧!

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!

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

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