0

我最近将我的按钮编码为与屏幕右侧对齐,它似乎不想工作。我还检查了我的父布局是否设置为覆盖屏幕的整个宽度,并且确实如此。

    public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    //Vertical Layout - For the layout of the newSheetLayout.
    newSheetLayoutV = new LinearLayout(this);
    newSheetLayoutV.setOrientation(LinearLayout.VERTICAL);

    //Horizontal layout - nested in Vertical layout. Used for the next, back and save buttons.
    newSheetButtonLayoutH = new LinearLayout(this);
    newSheetButtonLayoutH.setOrientation(LinearLayout.HORIZONTAL);

  //Vertical Layout - For the layout of the newSheetLayout.
    dataShowV = new LinearLayout(this);
    dataShowV.setOrientation(LinearLayout.VERTICAL);

    //Horizontal layout - nested in the dataShow V. Used for the data entry objects.
    dataShowH = new LinearLayout(this);
    dataShowH.setOrientation(LinearLayout.HORIZONTAL);

    //Next button.
    nextButton = new Button(this);
    nextButton.setText("NEXT >");
    nextButton.setOnClickListener(nextListener);

    //Back button.
    backButton = new Button(this);
    backButton.setText("< BACK");
    backButton.setOnClickListener(backListener);
    backButton.setEnabled(false);

    //Save button.
    saveButton = new Button(this);
    saveButton.setText("Save");
    saveButton.setOnClickListener(saveListener);
    saveButton.setEnabled(false);

    //Addition of the buttons to the button view in order that needs to be seen.
    newSheetButtonLayoutH.addView(backButton);
    newSheetButtonLayoutH.addView(nextButton);
    newSheetButtonLayoutH.addView(saveButton);

    //Layout Param's for the buttons.
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    newSheetButtonLayoutH.setLayoutParams(params); 

    //Set save to anchor right.
    LinearLayout.LayoutParams saveButtonParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    saveButtonParam.gravity = Gravity.RIGHT;
    saveButton.setLayoutParams(saveButtonParam);

    //Add all the layout Views to the main layout view.
    newSheetLayoutV.addView(dataShowV);
    newSheetLayoutV.addView(newSheetButtonLayoutH);
    newSheetLayoutV.setGravity(Gravity.BOTTOM);

    setContentView(newSheetLayoutV);
}

如果这是简单的事情并且我对解决方案非常盲目,我会相应地惩罚自己。

谢谢您的帮助。

4

3 回答 3

0

尝试设置layout_gravityofLinearLayout而不是你的Button.

于 2012-06-08T11:06:30.333 回答
0

尝试

saveButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,Gravity.RIGHT);

如果您正在使用 xml 并且您不需要以动态方式进行操作,那么请执行

android:layout_gravity="right"

编辑:我认为这就是你想要的

在此处输入图像描述

LinearLayout newSheetLayoutV,dataShowV,dataShowH,newSheetButtonLayoutH,newlayout;
    Button nextButton,backButton,saveButton;
    //RelativeLayout ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        newSheetLayoutV = new LinearLayout(this);
        newSheetLayoutV.setOrientation(LinearLayout.VERTICAL);

        //Horizontal layout - nested in Vertical layout. Used for the next, back and save buttons.
        newSheetButtonLayoutH = new LinearLayout(this);
        newSheetButtonLayoutH.setOrientation(LinearLayout.HORIZONTAL);
        newlayout = new LinearLayout(this);
        newlayout.setOrientation(LinearLayout.VERTICAL);
        newlayout.setGravity(Gravity.RIGHT);
        newlayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        //newSheetButtonLayoutH = new RelativeLayout(this);


      //Vertical Layout - For the layout of the newSheetLayout.
        dataShowV = new LinearLayout(this);
        dataShowV.setOrientation(LinearLayout.VERTICAL);

        //Horizontal layout - nested in the dataShow V. Used for the data entry objects.
        dataShowH = new LinearLayout(this);
        dataShowH.setOrientation(LinearLayout.HORIZONTAL);

        //Next button.
        nextButton = new Button(this);
        nextButton.setText("NEXT >");
        //nextButton.setOnClickListener(nextListener);

        //Back button.
        backButton = new Button(this);
        backButton.setText("< BACK");
        //backButton.setOnClickListener(backListener);
        backButton.setEnabled(false);

        //Save button.
        saveButton = new Button(this);
        saveButton.setText("Save");
        //saveButton.setOnClickListener(saveListener);
        saveButton.setEnabled(false);
        LinearLayout.LayoutParams saveButtonParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        //saveButtonParam.gravity = Gravity.RIGHT;
        saveButton.setLayoutParams(saveButtonParam);
        //Addition of the buttons to the button view in order that needs to be seen.

        newSheetButtonLayoutH.addView(backButton);

        newSheetButtonLayoutH.addView(nextButton);
        newlayout.addView(saveButton);
        newSheetButtonLayoutH.addView(newlayout);

        //Layout Param's for the buttons.
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        newSheetButtonLayoutH.setLayoutParams(params); 

        //Set save to anchor right.


        //Add all the layout Views to the main layout view.
        newSheetLayoutV.addView(dataShowV);
        newSheetLayoutV.addView(newSheetButtonLayoutH);
        newSheetLayoutV.setGravity(Gravity.BOTTOM);

        setContentView(newSheetLayoutV);

    }
于 2012-06-08T11:06:38.597 回答
0

尝试提供 RelativeLayout 参数

    RelativeLayout.LayoutParams rel_lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    rel_lp.addRule(RelativeLayout.ALIGN_RIGHT);
    rel_lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);      
    btn.setLayoutParams(rel_lp);    
于 2012-06-08T12:45:59.890 回答