【发布时间】:2011-03-19 10:29:27
【问题描述】:
使用主题或 ImageView ?
【问题讨论】:
标签: android image background
使用主题或 ImageView ?
【问题讨论】:
标签: android image background
在您的 xml 中使用 android:background 属性。如果要将其应用于整个活动,最简单的方法是将其放在布局的根目录中。所以如果你有一个 RelativeLayout 作为你的 xml 的开始,把它放在这里:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootRL"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background">
</RelativeLayout>
【讨论】:
您可以通过设置android:background xml 属性来将“背景图片”设置为活动,如下所示:
(例如,这里为一个活动设置一个线性布局并为布局设置一个背景图像(即间接到一个活动))
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/icon">
</LinearLayout>
【讨论】:
并且不要忘记在写完这些行之后清理你的项目,你会在你的 xml 文件中得到一个错误,直到你在 eclipse 中清理了你的项目:Project->Clean...
【讨论】:
将图片放在drawable文件夹中。可绘制文件夹位于 res 中。 drawable 有 5 种变体 可绘制的 hdpi 可绘制的 ldpi 可绘制的 mdpi 可绘制的 xhdpi 可绘制-xxhdpi
【讨论】:
现在我们必须使用match_parent:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/background">
</RelativeLayout>
【讨论】:
我们可以使用 ImageView 轻松地将背景图像放置在 PercentFrameLayout 中。我们必须设置 scaleType 属性 value="fitXY" 并且在前台我们还可以显示其他视图,例如 textview 或 button。
<android.support.percent.PercentFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<ImageView
android:src="@drawable/logo"
android:id="@+id/im1"
android:scaleType="fitXY"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
<EditText android:layout_gravity="center_horizontal"
android:hint="Enter Username"
android:id="@+id/et1"
android:layout_height="wrap_content"
app:layout_widthPercent="50%"
app:layout_marginTopPercent="30%"
/>
<Button
android:layout_gravity="center_horizontal"
android:text="Login"
android:id="@+id/b1"
android:layout_height="wrap_content"
app:layout_widthPercent="50%"
app:layout_marginTopPercent="40%"/>
</android.support.percent.PercentFrameLayout>
【讨论】:
ez方式:复制你的图片到这个位置:C:\Users\Your user name\AndroidStudioProjects\yourProjectName\app\src\main\res\drawable
并在您的 xml 文件中编写此代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/background">
</RelativeLayout>
在第 7 行写下您的文件名,例如 background
还有塔玛姆……
【讨论】: