新しいアプリケーションをセットアップしているときに、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();
}
}
ベストアンサー1
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"/>