---
title: "android程序在Layout中设置控件水平或垂直居中"
author: "EwinLive"
date: "2010-10-27T21:33:02Z"
tags: ["Android", "Eclipse"]
categories: ["移动开发"]
slug: "android-layout"
draft: false
lastmod: "2010-10-29T23:08:05Z"
id: 795942
url: "https://ewinlive.iteye.com/blog/795942"
---

要想让您的控件水平居中或垂直居中其实很简单，只要在控件的上一级中设置【android:gravity="center"】属性即可

如：

&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"  
    android:gravity="center"  
    android:background="#000000"  
    android:layout\_width="fill\_parent"  
    android:layout\_height="fill\_parent"  
    &gt;  
    &lt;ImageView  
    android:id="@+id/logo"  
    android:src="@drawable/logo"  
    android:layout\_width="wrap\_content"  
    android:layout\_height="wrap\_content"  
    /&gt;  
&lt;/LinearLayout&gt;

    这样一个ImageView控件就乖乖的待在你选定区域的正中间了。 gravity的属性值还有很多，可以单独设置水平或垂直居中。大家可以查看相应的文档，或使用Eclipse的提示功能快速查看。