【问题标题】:Android Lollipop not showing android:background imageAndroid Lollipop 不显示 android:background 图像
【发布时间】:2015-09-09 13:45:23
【问题描述】:

我设计了屏幕,我使用 JPEG 图像作为布局背景。完整的 UI 屏幕在 Android 8(GingerBread) 到 Android 19(Kitkat) 中正确可见。布局背景在 Android 20+ 中不可见(Lollipop)

我正在使用应用兼容性库

请看下面的截图

模拟器 Android 8


设备奇巧


设备棒棒糖


已经尝试过的解决方案

  1. 创建另一个文件夹 mipmap 并将相同的图像复制到那里并尝试使用@mipmap\image_background 访问它。 没有任何改变

布局 XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/mainBody"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<!-- android:background="@drawable/login_bg"  -->
    <View
        android:id="@+id/vwStruts"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerInParent="true" />


    <TableLayout
        android:id="@+id/tblLogin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="5dp"
        android:padding="5dp" >

        <TableRow
            android:id="@+id/tbrUserId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:gravity="center" >

            <EditText
                android:id="@+id/edtUserId"
                style="@style/EditText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:hint="@string/hintEmpId" >
                <requestFocus />
            </EditText>
        </TableRow>

        <TableRow
            android:id="@+id/tbrPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            >

            <EditText
                android:id="@+id/edtPassword"
                style="@style/EditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:ems="10"
                android:hint="@string/hintPassword"
                android:inputType="textPassword" />

        </TableRow>

        <TableRow
            android:id="@+id/tbrLoginAndClear"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center" >

            <Button
                android:id="@+id/btnLogin"
                style="@style/LoginButton"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minHeight="40dp"
                android:layout_weight="1"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:text="@string/login" />
        </TableRow>
    </TableLayout>

    <ImageView
        android:id="@+id/imgLogo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/tblLogin"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="@dimen/tblLoginMargin"
        android:src="@raw/footer_logo" />

</RelativeLayout>

styles.xml,values-v21 没有文件夹

 <resources xmlns:android="http://schemas.android.com/apk/res/android">

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.

    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.

        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

登录按钮可绘制

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- On Pressed state -->
    <item android:state_pressed="true" >
        <shape>
            <solid android:color="@color/btn_login_on_press" />         
        </shape>

     </item>

    <!-- enabled true Not pressed, normal state -->
    <item android:state_pressed="false" android:state_enabled="true">
         <shape>
            <solid android:color="@color/btn_login_normal" />           
        </shape>
    </item>

</selector>

编辑 1:添加 Styles.xml 和按钮可绘制对象 编辑 2:提及支持库的使用

【问题讨论】:

  • 从 values 文件夹和其他 values-v21 中发布您的 theme.xml 或 styles.xml。
  • 你上面的代码是完美的,我在 android L 设备和 M 模拟器中检查过相同的东西,可能你遇到了风格相关的问题。请分享您用于按钮的 style.xml 以及主要样式
  • @Shishram 立即查看。我添加了两种样式。 xml
  • @AndroidWeblineindia 也添加了按钮可绘制对象
  • @AkhilJain,尝试添加样式。对于 v21 以及测试。如果您愿意,我可以发布一个答案,其中将包含 21 个 API 级别以下和以上的样式。这样做可能会解决您的问题。

标签: android android-layout android-5.0-lollipop


【解决方案1】:

将此主题用于您在manifest.xml 中的活动

<style name="AppTheme" parent="Theme.AppCompat.Light">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/primaryColor</item>
        <item name="colorPrimaryDark">@color/primaryColor</item>
        <item name="colorAccent">@color/primaryColor</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:textColorPrimary">@android:color/white</item>
        <item name="actionMenuTextColor">@android:color/holo_blue_dark</item>
        <item name="android:actionMenuTextColor">@android:color/holo_blue_dark</item>
        <item name="android:windowBackground">@android:color/white</item>
        <item name="android:screenOrientation">portrait</item>
    </style>

对于以上 API 21,使用 res/values-v21 中的这个创建文件夹并在下面使用这个主题

<style name="AppTheme" parent="Theme.AppCompat.Light">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/primaryColor</item>
        <item name="colorPrimaryDark">@color/primaryColor</item>
        <item name="colorAccent">@color/primaryColor</item>
        <item name="android:textColorPrimary">@android:color/white</item>
        <item name="android:navigationBarColor">@android:color/black</item>
        <item name="actionMenuTextColor">@android:color/holo_blue_dark</item>
        <item name="android:actionMenuTextColor">@android:color/holo_blue_dark</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/black</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowBackground">@android:color/white</item>
        <item name="android:windowContentTransitions">true</item>
        <item name="android:screenOrientation">portrait</item>
    </style>

【讨论】:

  • 我试过了,我只是用了Theme.AppCompat.Light,没有任何改变。是的,我在清单中使用 AppTheme
  • 好吧!,因为我使用了你的布局,它适用于我的 API 级别高于或低于 21。所以如果不查看你的代码库,很难告诉你是什么导致了你的问题......跨度>
【解决方案2】:

假设您使用样式应用可绘制对象,您可能忘记在 v21 文件夹中编辑其他样式(如果存在)。

【讨论】:

  • 我也试过去掉样式直接应用android:background还是一样的结果
【解决方案3】:

mipmap 文件夹的目的是让 android 启动器能够选择不同的分辨率。所以把你的图像放回drawable文件夹。将图像放在背景中的更好位置是 this。主要思想是将背景层设置为登录活动的窗口资源。

【讨论】:

  • 我用 android:windowBackground 并没有太大帮助
  • @AkhilJain 没有人遇到同样的问题,所以原因在于您的代码,windowBackground 为您提供了更好的用户体验 - 活动加载时没有白屏......在新的 AndroidStudio 中,我看到了一些关于图像资源的警告,也许您需要将 JPEG 转换为 PNG,谁知道呢……
【解决方案4】:

经过长时间的测试和试用,我终于意识到..代码或xml或样式或主题设置没有任何问题。

问题堆栈中的小针是“防止位图太大而无法上传到纹理android”

原因,在屏幕OpenGLRenderer 上渲染图像时,没有为高分辨率设备加载位图,因此在 nexus 设备上没有图像。

我使用的解决方法是在onCreate() 方法中手动调整位图的大小并将其设置为布局的背景。

希望对无法加载背景的人有所帮助。

欢迎随时查询!!

【讨论】:

    【解决方案5】:

    除了以编程方式调整位图大小之外,对此的快速解决方法是在外部调整位图大小并将其放置在 v21 res 文件夹中并将其设置为视图的背景。如果对于图像在跨设备上的外观没有偏好,那么只需将其放回一般的可绘制文件夹中仍然可以工作。 除非您随机更改背景图像,否则在运行时手动调整位图的大小可能会很昂贵。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多