安卓棒棒糖版本不显示android:background图片

8
我设计了一个屏幕,其中使用了JPEG图像作为布局背景。完整的UI界面在Android 8(GingerBread)到Android 19(Kitkat)中正确显示。在Android 20+(Lollipop)中,布局背景不可见。
我正在使用应用兼容库。
请参见下面的截图。
模拟器 Android 8

screenshot of emulator android 8


设备套件

kitkat screenshot


设备棒棒糖

lollipop screenshot


已尝试的解决方案

  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文件夹中的themes.xml或styles.xml以及其他values-v21中的文件。 - Shishram
@AndroidWeblineindia 添加了按钮可绘制对象。 - Akhil Jain
你把背景图片放在了所有的DPI文件夹里吗?比如drawable-mdpi,drawable-hdpi,drawable-ldpi,drawable-xhdpi和drawable-xxhdpi。 - Ramesh Kumar
我将其放置在drawable默认目录中,这样所有的DPI都会落在该文件夹中。 - Akhil Jain
请问背景图片的分辨率是多少? - Rajesh Jadav
显示剩余6条评论
5个回答

3

假设您正在使用样式应用可绘制对象,如果存在v21文件夹中的附加样式,则可能忘记编辑它。


我也尝试过去除样式并直接应用 android:background,但结果仍然相同。 - Akhil Jain

3

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。 - Akhil Jain
1
好的!因为我已经使用了你的布局,并且在API级别高于或低于21的情况下它对我有效。所以如果没有查看你的代码库,很难告诉你是什么原因导致了你的问题... - Shishram

2

mipmap文件夹的目的是为了让Android启动器能够选择不同的分辨率。因此,请将您的图像放回drawable文件夹中。在您的背景中放置图像的更好方法是像这样做。主要思想是将您的背景层设置为登录活动的窗口资源。


我使用了 android:windowBackground,但效果并不是很好。 - Akhil Jain
@AkhilJain 没有人遇到相同的问题,所以原因在于你的代码,windowBackground可以提供更好的UX——在活动加载时没有白屏。在新的AndroidStudio中,我看到了一些有关图像资源的警告,也许你需要将JPEG转换为PNG,谁知道呢... - Viktor Yakunin

2
经过长时间的测试和试验,我终于意识到......代码、XML、样式或主题设置都没有问题。
问题在于堆栈中的一个小针头是“防止位图过大上传到纹理Android”。
原因是,在屏幕上渲染图像时,OpenGLRenderer没有为高分辨率设备加载位图,因此在Nexus设备上没有图像显示。
我使用的解决方法是在onCreate()方法中手动调整位图大小并将其设置为布局的背景。
希望这能帮助任何无法加载背景的人。
如有疑问,请随时查询更多信息!

0
除了在程序中调整位图大小之外,快速解决此问题的方法就是将位图外部调整大小并放置在v21 res文件夹中,并将其设置为视图的背景。如果没有关于图像在不同设备上应该如何显示的偏好,则将其放回一般的drawable文件夹中仍然可以工作。 在运行时手动调整位图大小可能会很昂贵,除非您随机更改背景图像。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接