Setting parameters on child views of a RelativeLayout

让人想犯罪 __ 提交于 2019-11-27 15:19:10
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!

Falmarri, you'll need to use the 'addRule(int)' method.

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams
    (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RIGHT_OF, first.getId());

The full list of constants that can be used for addRule can be found here: http://developer.android.com/reference/android/widget/RelativeLayout.html

And here is the addRule method reference: http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#addRule(int,%20int)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!