由于我的项目中的一些需求,我必须使用MaterialComponents,所以我以前的UI变得混乱,我需要改变它。
上一张带有 AppCompat 的图片:
带有 MaterialComponents 的新图像:
<style name="ButtonBorderless" parent="Base.Widget.AppCompat.Button.Borderless">
<item name="android:minHeight">0dp</item>
<item name="android:minWidth">0dp</item>
<item name="android:textSize">14sp</item>
</style>
Run Code Online (Sandbox Code Playgroud)
主要款式:
<style name="DayTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
Run Code Online (Sandbox Code Playgroud)
按钮:
<Button
android:id="@+id/btn_interval_decrease"
style="@style/ButtonBorderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:paddingRight="10dp"
android:paddingBottom="5dp"
android:text="-"
android:textColor="?attr/icon_color"
android:textSize="20dp" />
Run Code Online (Sandbox Code Playgroud)
请注意:此图中有两个问题:1.无边框按钮2.检查WE按钮,它的最后一部分正在切割
完整的 XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:cardCornerRadius="8dp"
android:background="?attr/cardbackgroundColor"
app:cardElevation="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/cardbackgroundColor">
<LinearLayout
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="?attr/cardbackgroundColor"
android:padding="15dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/cardbackgroundColor"
android:padding="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="@string/repeat"
android:textColor="?attr/day_colorDarkGray_night_colorWhite"
android:textSize="16sp"
android:textStyle="bold" />
<Switch …Run Code Online (Sandbox Code Playgroud) android android-button material-components material-components-android
看:
代码:
<RelativeLayout
android:id="@+id/circle_card_payment"
android:layout_width="40dp"
android:layout_height="40dp">
<com.google.android.material.button.MaterialButton
android:id="@+id/logo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:backgroundTint="@color/colorBlack"
android:insetLeft="0dp"
android:insetTop="0dp"
android:insetRight="0dp"
android:insetBottom="0dp"
android:text="W"
android:textColor="#FFFFFF"
android:textSize="16sp"
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay_ButtonCircle50"
app:strokeWidth="0dp" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
样式文件
<style name="ShapeAppearanceOverlay_ButtonCircle50">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">20dp</item>
</style>
Run Code Online (Sandbox Code Playgroud)
为了确认材质按钮包含完整空间,我将材质按钮颜色设置为黑色。并且里面的文字颜色是白色的。
有人可以根据我的情况给我确切的解决方案吗?请注意,您不应将相对布局大小从 40 dp 增加到 50 dp,我知道可以解决问题,但是 logo 大小会更大,这是不合适的。
请注意,我必须需要将此按钮包装在任何根布局中。那是强制性的。就像现在我的材质按钮在相对布局内。
我正在使用MaterialAlertDialogBuilder,因为我想要圆角和白天和夜间模式。请注意,我不能使用dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));线条,因为在我的风格中,我在 style.xml 中有不同的父级,我用它根据日/夜模式设置颜色。
问题:如何更改 MaterialAlertDialogBuilder 中的正/负按钮背景颜色?
请注意,可绘制背景不适用于MaterialAlertDialogBuilder
代码:
public void showNotesDialog() {
MaterialAlertDialogBuilder alertDialogBuilder = new MaterialAlertDialogBuilder(this, R.style.dialogBoxStyle);
LayoutInflater li = LayoutInflater.from(this);
View promptsView = li.inflate(R.layout.row_note_layout, null);
alertDialogBuilder.setView(promptsView);
etNote = promptsView.findViewById(R.id.et_note);
String buttonName = getString(R.string.add);
if (!getNotes.isEmpty()) {
buttonName = getString(R.string.update);
etNote.getText().clear();
etNote.setText(getNotes);
}
alertDialogBuilder.setPositiveButton(buttonName, null);
alertDialogBuilder.setNegativeButton(getString(R.string.cancel), null);
alertDialogBuilder.setNeutralButton("Clear", null);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.setCancelable(false);
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button buttonPositive = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
Button buttonNegative = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEGATIVE); …Run Code Online (Sandbox Code Playgroud)