---
title: "FrameLayout 配合ImageView 设置背景"
author: "何不笑"
date: "2012-09-25T21:31:42Z"
tags: []
categories: ["移动开发"]
slug: "framelayout-imageview"
draft: false
lastmod: "2012-09-25T21:31:42Z"
id: 1685386
url: "https://wait7758521.iteye.com/blog/1685386"
---

若以图片作为LinearLayout的背景，那么将无法控制LinearLayout的高度和宽度，其尺寸会不受控制的去适应图片的大小，因此采用FrameLayout 配合ImageView添加可控制大小的背景，xml的实现代码如下：

 

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/login\_main\_bg"

android:layout\_width="match\_parent"

android:layout\_height="match\_parent"

android:background="@drawable/login\_bg"

android:orientation="vertical" &gt;

 

&lt;!-- title bar --&gt;

 

&lt;FrameLayout

android:layout\_width="match\_parent"

android:layout\_height="@dimen/login\_title\_height" &gt;

 

&lt;ImageView

android:layout\_width="match\_parent"

android:layout\_height="match\_parent"

android:background="@drawable/login\_title\_bg" /&gt;

 

&lt;ImageView

android:layout\_width="@dimen/login\_title\_logo\_width"

android:layout\_height="match\_parent"

android:layout\_marginLeft="@dimen/login\_title\_logo\_marginLeft"

android:src="@drawable/login\_logo" /&gt;

&lt;!-- android:layout\_marginLeft="@dimen/login\_title\_logo\_marginLeft" --&gt;

&lt;/FrameLayout&gt;

 

… …

 

&lt;/LinearLayout&gt;

 

请注意红色标注部分，不可将"background"换成"src"属性，这样会引发ImageView无法完全填充FrameLayout。

 

 

 

![](/admin/blogs/D:%5Cclip_image001%2809-25-21-22-13%29.png)