Android 背景图像放置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2781593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:19:41  来源:igfitidea点击:

Background Image Placement

androidbackground-imageandroid-layout

提问by Ryan

I'm trying to set my root LinearLayout element (which is set to fill_parent in both dimensions) to have a background image which is always located in the lower left corner of the screen no matter the orientation of the device. It would be excellent if there were some way to set the background image position such as that which you can do with css using "background-position: left bottom;" but I'm not seeing a way to achieve this in Android. Is there a way to do this?

我正在尝试将我的根 LinearLayout 元素(在两个维度中都设置为 fill_parent)设置为具有始终位于屏幕左下角的背景图像,无论设备的方向如何。如果有某种方法可以设置背景图像位置,例如您可以使用“background-position: left bottom;”对 css 进行设置,那就太好了。但我没有看到在 Android 中实现这一目标的方法。有没有办法做到这一点?

Thanks for the help.

谢谢您的帮助。

回答by Yoni Samlan

You'll have to create a custom bitmap drawable with your bitmap in an XML file (eg "res/drawables/my_drawable.xml"

您必须使用 XML 文件中的位图创建自定义位图可绘制(例如“res/drawables/my_drawable.xml”

<?xml version="1.0" encoding="utf-8"?>
<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/my_png_file"
    android:gravity="bottom|left" />

And then set this drawable xml as your view's background ("@drawables/my_drawable"). The drawable XML format is very poorly documented in the Android site, though, so it's definitely not an easy problem to figure out how to solve on your own.

然后将此 drawable xml 设置为视图的背景(“@drawables/my_drawable”)。但是,Android 站点中对可绘制 XML 格式的记录非常少,因此要自己弄清楚如何解决绝对不是一个容易的问题。

回答by neteinstein

If found an advantage doing this programatically: the image fits the layout instead of being bigger then it. No ideia why.

如果发现以编程方式执行此操作的优势:图像适合布局而不是比布局更大。不知道为什么。

The code:

编码:

Bitmap bmp = BitmapFactory.decodeResource(getResources(),
                R.drawable.image);
BitmapDrawable bitmapDrawable = new BitmapDrawable(bmp);
bitmapDrawable.setGravity(Gravity.BOTTOM|Gravity.RIGHT);
LinearLayout layout = (LinearLayout) findViewById(R.id.background_root);
layout.setBackgroundDrawable(bitmapDrawable);

回答by sulai

Wrap your LinearLayout into a RelativeLayout and add an ImageView to it. Using android:layout_alignParentBottom and android:layout_alignParentLeft you can put your image to the lower left.

将您的 LinearLayout 包装成一个 RelativeLayout 并向其添加一个 ImageView。使用 android:layout_alignParentBottom 和 android:layout_alignParentLeft 您可以将图像放在左下方。

<RelativeLayout
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:src="@drawable/my_image" />

    <LinearLayout
        android:id="@+id/linearLayout"
        ... >

Make sure ImageView is the first element of RelativeLayout to draw the image in background.

确保 ImageView 是 RelativeLayout 的第一个元素以在背景中绘制图像。

回答by Chirag Joshi

My answer is just an update of above answers If you are using XML for background, it may occur problem in aspect ratio

我的回答只是以上答案的更新 如果您使用 XML 作为背景,则可能会出现纵横比问题

so use this code

所以使用这个代码

Bitmap bmp = BitmapFactory.decodeResource(getResources(),
                R.drawable.image);
BitmapDrawable bitmapDrawable = new BitmapDrawable(bmp);
  bitmapDrawable.setGravity(Gravity.BOTTOM| Gravity.RIGHT| Gravity.FILL_HORIZONTAL);
LinearLayout layout = (LinearLayout) findViewById(R.id.background_root);
layout.setBackgroundDrawable(bitmapDrawable);

Gravity.Fill_HORIZONTAL will fit with width of layout on which you are applying background you can also mention this in XML file but as i said it will occur issue of Image Ratio

Gravity.Fill_HORIZONTAL 将适合您应用背景的布局宽度,您也可以在 XML 文件中提及这一点,但正如我所说,它会出现图像比例问题