MaterialButton与Button的大小差异 [英] MaterialButton size difference to Button
问题描述
我正在设置一个新的应用程序,并遇到了materialButton的高度与我设置的大小不匹配的事实.因此,我尝试使用常规Button,就像你们在下面的屏幕截图中看到的那样.这些按钮的高度不同,尽管它们的高度与您在我的代码中看到的高度相同.
I' am setting up a new application and came across the fact that the materialButton's height does't match the size which i set. So i tried on the regular Button and as you guys can see in the screenshot below. The buttons have different height although they getting the same height as you can see in my code.
如何使用MaterialButton达到正常的高度?
How can i get normal height with MaterialButton?
谢谢.
公共类MainActivity扩展了AppCompatActivity {
public class MainActivity extends 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();
}
}
推荐答案
我相信MaterialButton顶部和底部的空间来自 android:insetTop
/ android:insetBottom
对我来说是 6dp
,包含材料组件1.0.0.我不确定如何以编程方式设置这些设置,但可以在xml中轻松完成:
I believe the space on top and bottom of the MaterialButton is coming from android:insetTop
/android:insetBottom
which is 6dp
for me with material components 1.0.0. I am not sure, how to set these programmatically, but it can be done easily in xml:
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:insetBottom="0dp"
android:insetTop="0dp"/>
这篇关于MaterialButton与Button的大小差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!