小编Apa*_*che的帖子

MaterialButton与Button的大小差异

我正在设置一个新的应用程序,并遇到了MaterialButton的高度与我设置的大小不匹配的事实。因此,我尝试使用常规Button,就像你们在下面的屏幕截图中看到的那样。这些按钮具有不同的高度,尽管它们的高度与您在我的代码中看到的高度相同。

如何使用MaterialButton获得正常的高度?

谢谢。

公共类MainActivity扩展了AppCompatActivity {

MaterialButton materialButton;
Button button;

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

    LinearLayout linearLayout = new LinearLayout(this);
    linearLayout.setId(getGeneratedId());

    setContentView(linearLayout);

    int buttonWidth = getResources().getDimensionPixelSize(R.dimen.buttonWidth);
    int buttonHeight = getResources().getDimensionPixelSize(R.dimen.buttonHeight);

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(buttonWidth, buttonHeight);

    materialButton = new MaterialButton(this);
    materialButton.setId(getGeneratedId());
    materialButton.setLayoutParams(layoutParams);
    materialButton.setBackgroundColor(Color.BLUE);
    materialButton.setText("MatrialB");
    materialButton.setTextColor(Color.WHITE);


    button = new Button(this);
    button.setId(getGeneratedId());
    button.setLayoutParams(layoutParams);
    button.setBackgroundColor(Color.RED);
    button.setText("Button");
    button.setTextColor(Color.WHITE);

    linearLayout.addView(materialButton);
    linearLayout.addView(button);

}

Integer getGeneratedId() {
    return ViewCompat.generateViewId();
}
Run Code Online (Sandbox Code Playgroud)

}

屏幕截图

java android material-design

1
推荐指数
2
解决办法
305
查看次数

标签 统计

android ×1

java ×1

material-design ×1