布局背景图像的问题
我正在尝试在布局中设置背景图像(通过 xml 或代码)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/my_image">
,或者
View myLayout= findViewById( R.id.main_layout);
myLayout.setBackgroundResource(R.drawable.my_image);
我的问题是 android 修复所有背景屏幕中的图像,调整大小并修改图像比例。
如何设置图像,并且图像只占用必要的空间? (没有调整图像大小)
I am trying to set a image like background image in a layout (by xml or by code)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/my_image">
or
View myLayout= findViewById( R.id.main_layout);
myLayout.setBackgroundResource(R.drawable.my_image);
My problem is than android fix the image in the all background screen, resizing and modifying the image ratio.
How can I set a image, and that the image takes only the neccesary space? (without image resizing)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在布局中不添加
ImageView
的情况下,如果图片小于空间,可以创建一个 xml 位图资源 如下所示,并使用它代替原始背景图像:Without adding an
ImageView
to the layout, if the image is smaller than the space, you can create an xml bitmap resource like the following, and use this in place of the original background image:将 LinearLayout 放入相对布局中,然后将 ImageView 添加到该相对布局中。
请注意,ImageView 是RelativeLayout 中的第一个元素,导致它在背景中绘制。通过在 ImageView 中声明 fitXY 和 adjustmentViewBounds,您可以确保图像保持其比例。您也可以关闭 adjustmentViewBounds 并选择其他scaleType,例如 fitStart 或 centerCrop。他们还保持纵横比。
Put your LinearLayout inside a RelativeLayout and add an ImageView to that RelativeLayout.
Note that ImageView is the first element in RelativeLayout, causing it to be drawn in background. By declaring fitXY and adjustViewBounds in ImageView, you ensure your image keeps its ratio. You could as well turn adjustViewBounds off and choose another scaleType like fitStart or centerCrop. They also keep aspect ratio.
只是在 LinearLayout 中采用另一种布局。
将其设置为相对,以便您可以在任何您想要的地方保留该布局。
并在相对布局中保留 imageView。
是的,这就是我想你想要的......
Jst take another layout inside LinearLayout.
Keet it as Relative so that u can keep that layout wherever You want.
and in that Relative Layout u keep imageView.
And ya that's it i think what u want....
“setBackground”调整图像大小以填充视图(LinearLayout)。
请在LinearLayout或RelativeLayout中添加ImageView,并且然后使用 setImageResource 为了实现这一点。
"setBackground" resize the image to fill the View(LinearLayout).
Please add ImageView in LinearLayout or RelativeLayout, and then use setImageResource to achieve that.