gpt4 book ai didi

java - MaterialButton 与 Button 的尺寸差异

转载 作者:行者123 更新时间:2023-12-02 11:41:20 24 4
gpt4 key购买 nike

我正在设置一个新的应用程序,并发现 Material 按钮的高度与我设置的尺寸不匹配。所以我尝试了常规按钮,正如你们在下面的屏幕截图中看到的那样。这些按钮具有不同的高度,尽管它们的高度与您在我的代码中看到的相同。

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

谢谢。

公共(public)类 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();
}

}

Screenshot

最佳答案

我相信 MaterialButton 顶部和底部的空间来自 android:insetTop/android:insetBottom 对我来说是 6dp使用 Material 组件 1.0.0。我不确定如何以编程方式设置这些,但可以在 xml 中轻松完成:

<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:insetBottom="0dp"
android:insetTop="0dp"/>

关于java - MaterialButton 与 Button 的尺寸差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56840355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com