java - MaterialButton 与 Button 的大小差异
问题描述
我正在设置一个新的应用程序,发现materialButton 的高度与我设置的大小不匹配。所以我尝试了常规按钮,正如你们在下面的屏幕截图中看到的那样。这些按钮具有不同的高度,尽管它们的高度与您在我的代码中看到的高度相同。
如何使用 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();
}
}
解决方案
我相信 MaterialButton 顶部和底部的空间来自android:insetTop
/android:insetBottom
这6dp
对我来说是材质组件 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"/>
推荐阅读
- dart - 颤振错误:错误:无法在字段初始化程序中访问“this”以读取“computeMaxScale”
- nginx - Nginx 负载平衡不起作用测试 aws 服务器
- angular - Mat-Dialog & Popover z-Index
- wireshark - Wireshark Corba stub 反编译
- c - 挂在 connect()
- aframe - 使用 aframe-inspector-plugin-recast 在 Aframe 中创建导航网格
- javascript - 测试需要音频响应的测验应用程序
- c# - VscrollBar 在 TextBox 多行中的位置
- lua - 检查名称是否在表中 [Lua]
- c++ - 如何在链表中找到循环开始的节点?