右对齐LinearLayout中的元素?

时间:2014-02-05 10:54:41

标签: android android-layout android-linearlayout

如何右对齐 水平 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);

1 个答案:

答案 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;