如何右对齐 水平 LinearLayout
的元素,而不必将其转换为RelativeLayout
或将其元素包装在其他布局中。
这是我在布局中使用的布局参数:
// this is for some elements(ImageButtons)
LinearLayout.LayoutParams lp_ineer_ver = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT, Gravity.RIGHT);
lp_ineer_ver.bottomMargin = 2;
lp_ineer_ver.leftMargin = 3;
lp_ineer_ver.topMargin = 2;
// this is for others(TextViews, Buttons)
LinearLayout.LayoutParams elements_params = (LinearLayout.LayoutParams)text1.getLayoutParams();
elements_params.gravity = Gravity.CENTER;
elements_params.leftMargin = 3;
elements_params.weight = 1;
text1.setLayoutParams(elements_params);
答案 0 :(得分:0)
试试这样..
LinearLayout.LayoutParams lp_ineer_ver = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
lp_ineer_ver.bottomMargin = 2;
lp_ineer_ver.leftMargin = 3;
lp_ineer_ver.topMargin = 2;
lp_ineer_ver.gravity= Gravity.RIGHT;