Jos*_*uel 8 android background-image
我试图在布局中设置像背景图像的图像(通过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">
Run Code Online (Sandbox Code Playgroud)
要么
View myLayout= findViewById( R.id.main_layout);
myLayout.setBackgroundResource(R.drawable.my_image);
Run Code Online (Sandbox Code Playgroud)
我的问题是android修复了所有背景屏幕中的图像,调整大小和修改图像比例.
如何设置图像,图像只占用必要的空间?(没有调整图像大小)
在不添加ImageView布局的情况下,如果图像小于空间,则可以创建如下所示的xml位图资源,并使用它来代替原始背景图像:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/background"
android:gravity="center"
/>
Run Code Online (Sandbox Code Playgroud)
将LinearLayout放在RelativeLayout中,并将ImageView添加到RelativeLayout.
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@drawable/my_image" />
<LinearLayout
android:id="@+id/main_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
Run Code Online (Sandbox Code Playgroud)
请注意,ImageView是RelativeLayout中的第一个元素,导致它在后台绘制.通过在ImageView中声明fitXY和adjustViewBounds,可以确保图像保持其比例.您也可以关闭adjustViewBounds并选择另一个scaleType,如fitStart或centerCrop.他们还保持纵横比.
| 归档时间: |
|
| 查看次数: |
20839 次 |
| 最近记录: |