【问题标题】:Android app layout background shows through controls in layoutAndroid 应用布局背景通过布局中的控件显示
【发布时间】:2015-04-03 17:48:36
【问题描述】:

我有一个带有 GridView 的 Android 应用程序,其中每个项目是两个按钮和两个 TextView 的 LinearLayout。当我将 LinearLayout 的背景颜色设置为白色时,按钮是灰色的。但是,如果我更改背景颜色,按钮的表面也会染上该颜色。我怎样才能防止这种情况发生?

强色示例:

按钮应该是浅灰色,而不是红灰色。

网格项目布局 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"
    android:background="@drawable/grid_cell_max">

    <Button
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:text="+"
        android:includeFontPadding="false"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        android:id="@+id/buttonPlus"
        android:layout_gravity="center_vertical"
        android:textSize="24sp"
        android:gravity="center_vertical|center_horizontal" />

    <Button
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:text="-"
        android:textSize="24sp"
        android:includeFontPadding="false"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        android:id="@+id/buttonMinus"
        android:layout_gravity="center_vertical"
        android:gravity="center_vertical|center_horizontal" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="8dp"
        android:paddingTop="1dp"
        android:paddingBottom="2dp"
        android:orientation="vertical">
        <TextView
            android:id="@+id/buildingTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:lines="1"
            android:shadowDx="1"
            android:shadowDy="1"
            android:shadowRadius="2"
            android:shadowColor="@android:color/darker_gray"
            android:ellipsize="end"
            android:textSize="15sp"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/buildingInfo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="11sp"
            android:lines="2"
            android:ellipsize="end"
            android:lineSpacingExtra="-2dp"/>
    </LinearLayout>
</LinearLayout>

drawable/grid_cell_max.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape= "rectangle"  >
    <solid android:color="#ffe6ffe3"/>
    <stroke android:width="1dp"  android:color="#c0c0c0"/>
</shape>

【问题讨论】:

  • 尝试为按钮本身添加所需的背景颜色
  • 我试过了,但它破坏了按钮的 3D 效果,只显示一个大的灰色矩形。
  • 你用的是哪个主题??
  • Theme.Holo.Light,没有自定义。
  • 添加了来自 Android Studio 布局编辑器的图像来演示问题。

标签: android android-layout background-color


【解决方案1】:

不幸的是,我认为没有简单的方法可以做到这一点。我解决这个问题的方法是:

  1. 使用Android holo colors generator 生成新资源:

    • 确保选择按钮所需的颜色(在“主题名称”下)
    • 确保将“彩色按钮”切换为“是”
  2. 下载存档并解压。

  3. 仅将解压缩项目中的可绘制文件夹合并到您的文件夹中(仅按钮图像资源和按钮选择器 xml 文件 - 位于可绘制对象中)

  4. 将按钮的背景设置为:

    android:background="@drawable/apptheme_btn_default_holo_light"

您现在将拥有一个不透明按钮,其颜色是您从 android holo 颜色生成器中选择的。

如果您希望所有应用按钮的行为方式相同,则必须更新应用主题。为此,请按照上述步骤操作,然后更新您的“styles.xml”文件:

<style name="AppTheme" parent="android:Theme.Holo.Light">
   <item name="android:buttonStyle">@style/ButtonAppTheme</item>
</style>

<style name="ButtonAppTheme" parent="android:Widget.Holo.Light.Button">
   <item name="android:background">@drawable/apptheme_btn_default_holo_light</item>
</style>

此外,如果您这样做,您可以从 xml 中删除按钮背景。如果您想保留“Theme.Holo.Light”应用主题,我找不到其他解决方案。

【讨论】:

  • 谢谢,这个解决方案部分有效。普通按钮现在看起来不错,但是生成器为禁用状态创建了一个透明图像,因此背景颜色仍然通过禁用按钮显示。你知道有什么办法可以摆脱它吗?
  • 您可以生成一组新的图像,颜色为深灰色,并将 disabled/disabled_focused 替换为新的 normal/normal_focused
猜你喜欢
  • 1970-01-01
  • 2011-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-16
  • 1970-01-01
  • 1970-01-01
  • 2013-10-02
相关资源
最近更新 更多