Can I Set “android:layout_below” at Runtime Programmatically?

前端 未结 4 2097
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 11:06

Is it possible when creating a RelativeLayout at runtime to set the equivalent of android:layout_below programmatically?

4条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 11:46

    Yes:

    RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT); 
    params.addRule(RelativeLayout.BELOW, R.id.below_id);
    viewToLayout.setLayoutParams(params);
    

    First, the code creates a new layout params by specifying the height and width. The addRule method adds the equivalent of the xml properly android:layout_below. Then you just call View#setLayoutParams on the view you want to have those params.

提交回复
热议问题