Android自定义Drawable背景资源失效求助:登录按钮样式不生效
Hey there! Let's troubleshoot why your custom rounded-border transparent background isn't showing up on your login button. I've reviewed your code and found a couple of key issues to address:
1. Fix the Drawable Reference in Layout
First off, a quick typo in your layout: your login button (main_login_btn) is currently using @drawable/buttons as its background, but you mentioned it should use input_design.xml. Correct that reference first:
<Button android:id="@+id/main_login_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentStart="true" android:layout_marginBottom="10dp" android:layout_marginStart="15dp" android:layout_marginEnd="15dp" android:background="@drawable/input_design" <!-- Replace "buttons" with "input_design" --> android:padding="20dp" android:textAllCaps="false" android:textSize="18sp" android:text="Already have a account ? Login" android:textColor="@android:color/white" />
2. Resolve MaterialButton Compatibility Issues (For Android 5.0+)
If fixing the reference doesn't work, the problem likely stems from Android's default MaterialButton behavior. Starting from API 21, the standard Button is replaced with MaterialButton, which applies its own default style that can override your custom android:background setting. Here are 3 ways to fix this:
Option 1: Use AppCompatButton instead of Button
Switch your <Button> tags to <androidx.appcompat.widget.AppCompatButton> to ensure consistent behavior across Android versions and avoid MaterialButton's default style conflicts:
<androidx.appcompat.widget.AppCompatButton android:id="@+id/main_login_btn" ... android:background="@drawable/input_design" ... />
Option 2: Disable Background Tinting
Add android:backgroundTint="@null" to your Button to disable MaterialButton's default background coloring, letting your custom Drawable take effect:
<Button android:id="@+id/main_login_btn" ... android:background="@drawable/input_design" android:backgroundTint="@null" <!-- Add this line --> ... />
Option 3: Update Your App's Theme
Modify your app's theme (in res/values/styles.xml) to use the AppCompat button style globally:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar"> <!-- Other theme attributes --> <item name="android:buttonStyle">@style/Widget.AppCompat.Button</item> </style>
3. Verify Drawable Placement
Double-check that input_design.xml is saved in the res/drawable directory (not a version-specific subfolder like drawable-v21), so it's accessible to all Android versions your app supports.
Once you apply these fixes, your login button should display the transparent background with a 3dp white rounded border as expected!
内容的提问来源于stack exchange,提问作者Dhananjay pathak

