使用 ImageView 作为 Android 布局的背景
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20928748/
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
Using ImageView as background for an Android Layout
提问by RTF
I want to take advantage of the scaleType property, which I cannot use if I set my image using the background property on the LinearLayout
. I've tried using a LinearLayout
with 2 children: the first is an ImageView
(for the background image) and the second child is another LinearLayout
containing TextViews
, Buttons, etc. But the result is just the image followed by the other views.
我想利用 scaleType 属性,如果我使用LinearLayout
. 我试过使用LinearLayout
2 个孩子:第一个是ImageView
(用于背景图像),第二个孩子是另一个LinearLayout
包含TextViews
,按钮等。但结果只是图像后跟其他视图。
Is it possible to float the nested LinearLayout
over the ImageView
somehow? I know that there's lots of variations on this question around, but I haven't found anything that's helped me yet. If I've missed something, please point me in that direction. Also, I'd like to do this in XML if possible, and I'm flexible about what type of layout I can use (it doesn't have to Linear).
是否有可能浮在嵌套LinearLayout
在ImageView
不知何故?我知道这个问题有很多变化,但我还没有找到任何对我有帮助的东西。如果我错过了什么,请指出我的方向。此外,如果可能,我想在 XML 中执行此操作,并且我对可以使用的布局类型很灵活(它不必是线性的)。
回答by Rick Falck
I did it thusly using a RelativeLayout as the parent:
我这样做是使用 RelativeLayout 作为父级:
<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).
我使用了一种样式,因为 alpha 设置不适用于较低的 API(不记得临时限制是什么)。
回答by Blasanka
I cannot use above because of I have adapter and images are set from their to display in GridView
. So, in 2019 Im doing like below:
我无法在上面使用,因为我有适配器,并且图像是从它们设置为在GridView
. 所以,在 2019 年,我的做法如下:
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center">
<your_package_name.directory.SquareImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:layout_centerInParent="true"
android:id="@+id/gridImageView"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:paddingStart="6dp"
android:paddingEnd="6dp"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:textColor="@android:color/white"
android:background="@drawable/shape_image_holder_text"
android:alpha=".85"
android:scaleType="centerCrop"
android:textSize="24sp"/>
</RelativeLayout>
The key attribute for Stacking view here is android:layout_centerInParent="true"
.
这里 Stacking 视图的关键属性是android:layout_centerInParent="true"
。
Also note that I created android:background="@drawable/shape_image_holder_text"
to get some corner radius around my text view with some backdrop. Here is that xml:
另请注意,我创建的目的android:background="@drawable/shape_image_holder_text"
是在我的文本视图周围使用一些背景获得一些圆角半径。这是那个xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#22000000" />
<stroke
android:color="#33000000"
android:alpha="0.6"
android:width="1dp" />
<corners android:radius="12dp" />
</shape>
</item>
</selector>
To make my image adjust to height and width to same size in GridView. I created a custom class for SquareImageView
(You can use default ImageView
if you are not using GridView
or if you don't care about this).
使我的图像在 GridView 中将高度和宽度调整为相同的大小。我创建了一个自定义类SquareImageView
(ImageView
如果你不使用GridView
或者你不关心这个,你可以使用默认值)。
My SquareImageView.java file:
我的 SquareImageView.java 文件:
import android.content.Context;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
public class SquareImageView extends AppCompatImageView {
public SquareImageView(Context context) {
super(context);
}
public SquareImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//noinspection SuspiciousNameCombination
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
public SquareImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
It look like this:
它看起来像这样: