将layout_gravity设置为右

时间:2014-10-31 11:06:35

标签: android android-linearlayout

我想将引力设置在我的布局右侧,但它不能正常工作: (以下代码在循环内)

        LinearLayout lin  = new LinearLayout(getActivity());
        LayoutParams lin_parms = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
        lin_parms.gravity = Gravity.RIGHT;
        lin.setLayoutParams(lin_parms);
        lin.setGravity(Gravity.RIGHT);

        LayoutParams txt_parms = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
        txt_parms.gravity = Gravity.RIGHT;


        TextView txt_view = new TextView(getActivity());
        txt_view.setLayoutParams(txt_parms);
        txt_view.setTypeface(font);
        txt_view.setText("hello");

        lin.addView(txt_view);

        lay_lin_list_tractions.addView(lin);

enter image description here

1 个答案:

答案 0 :(得分:2)

TextView参数设为WRAP_CONTENT

LayoutParams txt_parms = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);