Android Lollipop not showing android:background im

2019-04-27 01:03发布

问题:

I have designed screen where I have used JPEG image as layout background. The complete UI screen is visible correctly in Android 8(GingerBread) to Android 19(Kitkat). The layout background is not visible in Android 20+(Lollipop)

I am using App Compatibility Library.

See the screenshot below

Emulator Android 8


Device Kitkat


Device Lollipop


Solutions already tried

  1. Create another folder mipmap and copied the same image there and try to access it using @mipmap\image_background. Nothing changed.

Layout 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, there is NO folder for 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>

login button drawable

<?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>

EDIT 1 : Added Styles.xml and button drawables EDIT 2 : Mention usage of support library

回答1:

Use this theme for your activity in 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>

and for above API 21 use this create folder in res/values-v21 and use this below theme

<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>


回答2:

Assuming that you're applying your drawable using style, you probably forgot to edit additional style in v21 folder if it exists.



回答3:

The purpose of mipmap folder is to give android launcher ability to choose different resolution. So put your image back into drawable folder. the better place to put your image within your background is do like this. The main idea is to set your background layer as window resource for login activity.



回答4:

After long time of test and trial, I finally realized..there is nothing wrong with the code or xml or styling or theme settings.

The little needle in the stack of issue was "Prevent bitmap too large to be uploaded into a texture android"

Reason, when rendering image on Screen OpenGLRenderer was not loading bitmap for high resolution devices and hence no image was coming on nexus devices.

The work around I used was to manually resize the bitmap progrmatically in onCreate() method and set as background for the layout.

Hope it helps for anyone who is not able to load background.

Feel free to query for more!!



回答5:

A quick fix for this in addition to programmatically resizing the bitmap is just to resize the bitmap externally and place it in a v21 res folder and set it to the background of your view. If there is no prefernce as to how the image should look like accross devices, then just putting it back into the general drawable folder should still work. Manually resizing the bitmap at runtime might be costly unless you randomly change the background image.