为什么setMargins()在按钮上不起作用?

时间:2018-11-07 19:11:36

标签: android

为什么setMargins()在按钮上不起作用?

Screen

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    GridLayout gridLayout = new GridLayout(this);
        gridLayout.setRowCount(5);
        gridLayout.setColumnCount(7);
    setContentView(gridLayout);

    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(100,100);
        layoutParams.setMargins(10,10,10,10);

    for(int i=1;i<31;i++) {
        Button button = new Button(this);
            button.setText(String.valueOf(i));
            button.setTextSize(18);
            button.setBackgroundResource(R.drawable.button);
        gridLayout.addView(button,layoutParams);
    }



}
}

1 个答案:

答案 0 :(得分:0)

如果在布局中使用ConstraintLayout,则应确保按钮从要有边距的那一侧开始有链条 例子

SELECT vaea_n_3, vaea_n_12, vaea_n_18,
    (SELECT CAST(jn_our_ref AS FLOAT) FROM job_number WHERE jn_id='20') as SubQuery,
    ROUND((vaea_n_12 + vaea_n_18) * (SELECT CAST(jn_our_ref AS FLOAT) FROM job_number WHERE jn_id='20'),2) AS Rounded,
    CASE WHEN vaea_n_3 <> ROUND((vaea_n_12 + vaea_n_18) * (SELECT CAST(jn_our_ref AS FLOAT) FROM job_number WHERE jn_id='20'),2) THEN 1 ELSE 0 END As Different

FROM variant_external_analysis
JOIN variant_detail ON vad_id = vaea_vad_id
WHERE vad_variant_code BETWEEN 1000 AND 990000 AND
    ( vaea_n_3 IS NULL OR
      vaea_n_3 <> ROUND((vaea_n_12 + vaea_n_18) * 
                        (SELECT CAST(jn_our_ref AS FLOAT) FROM job_number WHERE jn_id='20'),2)
    )

如果你不写

<Button
    android:id="@+id/btn"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
 />

在Button的标签中,您不能从底部设置边距 像这样的另一面。