问题
Because of some requirement in my project, I must use MaterialComponents, so my previous UI is became messy, and I need to change it.
previous Image with AppCompat:
new Image with MaterialComponents:
Code:
<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>
MainStyle:
<style name="DayTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
Button:
<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" />
Please note: There are two problems in this image: 1. borderless button 2.check the WE button, it's last part is cutting
Complete 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
android:id="@+id/switch_repeat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:paddingLeft="18dp"
android:paddingRight="16dp" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/cardbackgroundColor">
<LinearLayout
android:id="@+id/layout_repeat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:background="?attr/cardbackgroundColor"
android:orientation="vertical"
android:padding="15dp">
<androidx.cardview.widget.CardView
android:id="@+id/card_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="@drawable/selector_tab_reminder_type"
app:cardElevation="0dp">
<RelativeLayout
android:id="@+id/tab_relative_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/selector_tab_reminder_type">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="30dp"
android:clipToPadding="true"
app:tabBackground="@drawable/selector_tab_reminder_type"
app:tabGravity="fill"
app:tabIndicatorColor="@null"
app:tabMode="fixed"
app:tabPaddingBottom="0dp"
app:tabPaddingEnd="0dp"
app:tabPaddingStart="0dp"
app:tabPaddingTop="0dp"
app:tabSelectedTextColor="@color/colorWhite"
app:tabTextAppearance="@style/TabLayoutTextAppearance">
<com.google.android.material.tabs.TabItem
android:id="@+id/tab_daily"
style="@style/tabItem"
android:text="@string/tab_daily" />
<com.google.android.material.tabs.TabItem
android:id="@+id/tab_weekly"
style="@style/tabItem"
android:text="@string/tab_weekly" />
<com.google.android.material.tabs.TabItem
android:id="@+id/tab_monthly"
style="@style/tabItem"
android:text="@string/tab_monthly" />
<com.google.android.material.tabs.TabItem
android:id="@+id/tab_yearly"
style="@style/tabItem"
android:text="@string/tab_yearly" />
</com.google.android.material.tabs.TabLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<include
android:id="@+id/weekdays_error"
layout="@layout/row_error_textview" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="?attr/cardbackgroundColor">
<TextView
android:id="@+id/tv_repeat_interval"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="@string/repeat_daily"
android:textColor="?attr/day_colorDarkGray_night_colorWhite"
android:textSize="16sp" />
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
app:cardBackgroundColor="?attr/cardbackgroundColor"
app:cardCornerRadius="5dp"
app:cardElevation="0dp">
<LinearLayout
android:layout_width="68dp"
android:layout_height="match_parent"
android:background="@drawable/bg_repeat_reminder"
android:orientation="horizontal">
<Button
android:id="@+id/btn_interval_increase"
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" />
<View
android:layout_width="1dp"
android:layout_height="fill_parent"
android:background="?attr/toolbar_color" />
<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" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="?attr/cardbackgroundColor">
<Spinner
android:id="@+id/spin_list"
style="@style/Widget.AppCompat.Spinner.Underlined"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginEnd="8dp"
android:backgroundTint="?attr/icon_color"
android:dropDownVerticalOffset="17dp"
android:spinnerMode="dropdown" />
<TextView
android:id="@+id/tv_end_date"
style="@style/ButtonBorderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:hint="YYYY-MM-DD"
android:text=""
android:textColor="?attr/day_colorDarkGray_night_colorWhite"
android:visibility="visible" />
</RelativeLayout>
<TextView
android:id="@+id/error_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginBottom="8dp"
android:orientation="vertical"
android:textColor="@color/colorRed"
android:visibility="gone" />
<LinearLayout
android:id="@+id/layout_days"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/cardbackgroundColor"
android:orientation="vertical"
android:visibility="gone">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:background="?attr/cardbackgroundColor">
<ToggleButton
android:id="@+id/toggleSunday"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@drawable/selector_day_toggle"
android:gravity="center"
android:padding="8dp"
android:textColor="@color/selector_day_text"
android:textOff="SU"
android:textOn="SU"
android:textSize="16dp" />
<ToggleButton
android:id="@+id/toggleMonday"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@drawable/selector_day_toggle"
android:gravity="center"
android:padding="8dp"
android:textColor="@color/selector_day_text"
android:textOff="MO"
android:textOn="MO"
android:textSize="16dp" />
<ToggleButton
android:id="@+id/toggleTuesday"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@drawable/selector_day_toggle"
android:gravity="center"
android:padding="8dp"
android:textColor="@color/selector_day_text"
android:textOff="TU"
android:textOn="TU"
android:textSize="16dp" />
<ToggleButton
android:id="@+id/toggleWednesday"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@drawable/selector_day_toggle"
android:gravity="center"
android:padding="8dp"
android:textColor="@color/selector_day_text"
android:textOff="WE"
android:textOn="WE"
android:textSize="16dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp">
<ToggleButton
android:id="@+id/toggleThursday"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@drawable/selector_day_toggle"
android:gravity="center"
android:padding="8dp"
android:textColor="@color/selector_day_text"
android:textOff="TH"
android:textOn="TH"
android:textSize="16dp" />
<ToggleButton
android:id="@+id/toggleFriday"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@drawable/selector_day_toggle"
android:gravity="center"
android:padding="8dp"
android:textColor="@color/selector_day_text"
android:textOff="FR"
android:textOn="FR"
android:textSize="16dp" />
<ToggleButton
android:id="@+id/toggleSaturday"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@drawable/selector_day_toggle"
android:gravity="center"
android:padding="8dp"
android:textColor="@color/selector_day_text"
android:textOff="SA"
android:textOn="SA"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
<View style="@style/view_style" />
</LinearLayout>
<FrameLayout
android:id="@+id/layoutOverlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@id/layout_repeat"
android:layout_alignBottom="@id/layout_repeat"
android:background="@color/colorWhiteTransparent"
android:visibility="visible" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
回答1:
You have to use MaterialButtonToggleGroup
for best practice if you use Material Components
https://medium.com/over-engineering/hands-on-with-material-components-for-android-buttons-76fa1a92ec0a
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggleGroup2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
app:checkedButton="@id/btnA"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:singleSelection="true">
<com.google.android.material.button.MaterialButton
android:id="@+id/btnA"
style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:strokeColor="@color/colorPrimary"
app:backgroundTint="@android:color/transparent"
android:text="+" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btnS"
style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:strokeColor="@color/colorPrimary"
android:text="-" />
</com.google.android.material.button.MaterialButtonToggleGroup>
来源:https://stackoverflow.com/questions/58303666/what-is-alternative-of-appcompat-button-borderless-in-materialcomponents