【问题标题】:Drawable behaves differently in different activity layouts in Android StudioDrawable 在 Android Studio 的不同活动布局中表现不同
【发布时间】:2021-01-28 10:29:05
【问题描述】:

我有以下drawable:custom_yellow_button.xml在drawable文件夹中

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="5dp"/>
            <solid android:color="@color/yellow"/>
        </shape>
    </item>
</selector>

我有这两种不同的布局,它们都使用按钮:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    tools:context=".LogInActivity">

    <Button
        android:id="@+id/login_button"
        android:layout_width="275dp"
        android:layout_height="35dp"
        android:layout_marginTop="20dp"
        android:background="@drawable/custom_yellow_button"
        android:fontFamily="@font/courierprime_regular"
        android:includeFontPadding="false"
        android:text="Log In"
        android:textColor="#000000"
        android:textSize="21sp"
        app:backgroundTintMode="add" />

</androidx.constraintlayout.widget.ConstraintLayout>

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".RegistrazioneActivity">

    <Button
        android:id="@+id/login_button"
        android:layout_width="275dp"
        android:layout_height="35dp"
        android:background="@drawable/custom_yellow_button"
        android:fontFamily="@font/courierprime_regular"
        android:includeFontPadding="false"
        android:text="Log In"
        android:textColor="#000000"
        android:textSize="21sp" />

</androidx.constraintlayout.widget.ConstraintLayout>

在第一个布局中,它看起来像这样,这就是我想要的:

在第二个布局中,它看起来像这样:

.

谁能告诉我为什么?如果需要,我可以提供更多信息。

【问题讨论】:

  • 你可以删除这个应用程序:backgroundTintMode="add" 并检查它是否仍然是粉红色的吗?如果是这样,您可以发布两个布局文件吗?
  • 如果我这样做,它会变成紫色。我正在添加布局。
  • 两种布局都可以吗?一个是黄色按钮,第二个是粉红色按钮?
  • 请提供完整的 xml 文件。可能是你给父布局一些冲突的色调或颜色
  • 我发布了它们,我删除了一些不相关的文本字段,因为问题不是因为它们出现在布局上

标签: android xml android-studio button android-drawable


【解决方案1】:

我不确定您使用的是什么主题,但使用 MaterialThemes 您可以做到这一点,而且很容易。首先检查这个:https://material.io/components/buttons

然后,更改您的应用主题以扩展其中一个材料主题,如下所示:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">

然后添加实现:

implementation 'com.google.android.material:material:1.2.1'

之后,您可以简单地将其用作 XML 中的按钮:

<com.google.android.material.button.MaterialButton/>

在这个View 中,您的按钮有很多属性。只需检查上面的链接并查看。对于你的,我会选择这样的东西:

 <com.google.android.material.button.MaterialButton
     android:layout_width="match_parent"
     android:layout_height="60dp"
     android:textAlignment="center"
     android:textAllCaps="true"
     android:padding="15dp"
     android:backgroundTint="@color/yellow" //add background color
     app:rippleColor="@color/colorPrimary"  //ripple color - when button is pressed
     style="@style/Widget.MaterialComponents.Button.TextButton" //material component style, many options to choose from
     android:textColor="@color/colorPrimaryDark"
     app:cornerRadius="15dp" //corner radius, so you can adjust how much oval you want your corners to be/>

你会得到这样的结果:

【讨论】:

  • 这看起来是一个很好的实现,但我不明白为什么我制作的按钮在一个布局中可以完美工作,而在另一个布局中相同的按钮却不能。
  • 可能是你的主题问题或布局问题。可能是您在这两项活动中根本没有相同的主题。您可以在 AndroidManifest.xml 文件中检查这一点。但是由于 MaterialComponents 是设计应用程序的好方法,而且不必担心太多这些东西,只需使用它即可。它很棒,很简单,而且您在文档中拥有所需的一切。
【解决方案2】:

请参阅 AndroidManifest.xml 并确保为他们分配了相同的主题。
XML 编辑器的预览也可以切换到所有可用的主题,包括自定义主题。

当您向 XML 编辑器提供布局的 Context 时,如下所示:

xmlns:tools="http://schemas.android.com/tools"
tools:context=".activity.MainActivity"

XML 编辑器将为预览选择该主题 - 无需切换它们。

【讨论】:

    猜你喜欢
    • 2013-09-04
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2020-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多