Using ImageView as background for an Android Layout

≯℡__Kan透↙ 提交于 2019-12-03 08:16:51
Rick Falck

I did it thusly using a RelativeLayout as the parent:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#000000" >

    <ImageView
        style="@style/BackGroundImageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="42dp"
        tools:ignore="ContentDescription" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <!--Rest of widgets......-->

    </LinearLayout>

</RelativeLayout>

My Style for it:

<style name="BackGroundImageView">
    <item name="android:alpha">0.5</item>
    <item name="android:scaleType">fitXY</item>
    <item name="android:src">@drawable/img_background</item>
</style>

I used a style, because the alpha setting doesn't work on lower APIs (can't remember what the limit is offhand).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!