在RelativeLayout的子视图设置参数视图、参数、RelativeLayout

2023-09-12 08:07:09 作者:栀沫メ

我无法找到正是我需要使用设置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);
    }

}
 
RelativeLayout 相对布局

您只需要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!