以编程方式设置边距不起作用

时间:2016-09-09 15:20:23

标签: android android-button

我正在使用Android版4.4.2的物理设备开发原生Android项目。我在垂直线性布局中有一个按钮,我想以编程方式将顶部和底部边距设置为0。

有关父视图的信息

LinearLayout.LayoutParams detailParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
);
LinearLayout detailContainer = new LinearLayout(this);
detailContainer.setOrientation(LinearLayout.VERTICAL);
detailContainer.setLayoutParams(detailParams);

按钮

    LinearLayout.LayoutParams messageParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
        );
        messageParams.setMargins(10, 0, 10, 0);

    // Displays the message
    Button buttonWithMessage= new Button(this);
    buttonWithMessage.setText(post.getMessage());
    ...
    buttonWithMessage.setLayoutParams(messageParams);

将按钮添加到父视图

detailContainer.addView(buttonWithMessage);

但无论我在messageParams.setMargins(10, 0, 10, 0);中放置什么值,边距都不会改变。我希望边距跨越整个宽度,除了左边和右边的一点点,顶部和底部没有,所以它似乎触摸。我甚至尝试为上面的每个父视图设置这些相同的参数,因为父视图都是线性布局但仍然没有。

3 个答案:

答案 0 :(得分:0)

您应该使用LayoutParams来设置按钮边距:

 LayoutParams params = new LayoutParams(
    LayoutParams.WRAP_CONTENT,      
    LayoutParams.WRAP_CONTENT
 );
 params.setMargins(left, top, right, bottom);
 yourbutton.setLayoutParams(params);

根据您使用的布局,您应该使用RelativeLayout.LayoutParams或LinearLayout.LayoutParams。

答案 1 :(得分:0)

尝试这样的事情

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);


    LinearLayout.LayoutParams detailParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
    );
    LinearLayout detailContainer = new LinearLayout(this);
    detailContainer.setOrientation(LinearLayout.VERTICAL);
    detailContainer.setLayoutParams(detailParams);

    detailContainer.setBackgroundColor(Color.BLACK);
    LinearLayout.LayoutParams messageParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
    );
    messageParams.setMargins(400, 400, 400, 400);

    // Displays the message
    Button buttonWithMessage = new Button(this);
    buttonWithMessage.setText("Hai");
    buttonWithMessage.setTransformationMethod(null);
    //   buttonWithMessage.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
    buttonWithMessage.setTextColor(Color.BLACK);
    buttonWithMessage.setBackgroundColor(Color.RED);

    buttonWithMessage.setLayoutParams(messageParams);
    detailContainer.addView(buttonWithMessage);
    setContentView(detailContainer);
}

}

答案 2 :(得分:0)

你使用错误的LayoutParams for Button,这是有用的

    RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.rel_test);
    Button buttonWithMessage= new Button(this);
    buttonWithMessage.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT));
    RelativeLayout.LayoutParams test = (RelativeLayout.LayoutParams) buttonWithMessage.getLayoutParams();
    test.setMargins(10, 0, 0, 0);
    buttonWithMessage.setLayoutParams(test);
    buttonWithMessage.setText("TEST");
    relativeLayout.addView(buttonWithMessage);