---
title: "Android学习笔记之界面控件大小dip及布局管理器嵌套使用"
author: "250367976"
date: "2013-05-23T11:57:24Z"
tags: []
categories: ["移动开发"]
slug: "android-dip"
draft: false
lastmod: "2013-05-23T11:57:24Z"
id: 1874016
url: "https://bgc250.iteye.com/blog/1874016"
---

做Android也有三个星期了，但还没有做项目，一直在做一个练习的Android英语词典，在做的过程中也

遇到过很多问题，今天就来总结一下；因为问题比较多，就分类来总结吧……首先刚开始做的时候，肯

定是做界面啦，不过英语词典里面有一个卡片模式，搞得我很蛋疼，，因为我只知道用LinearLayout，

而用LinearLayout做出那个效果真的有点难，所以我研究了几天，也做了很多尝试，后来终于做出来了

：![](http://dl2.iteye.com/upload/attachment/0084/8942/f0d6883d-29c5-39b7-921c-8733e4525bc1.jpg)

xml代码：

```
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent" 
	android:background="@android:color/white"
	android:cacheColorHint="#000000" 
	android:layout_height="match_parent"
	android:orientation="vertical">
	<LinearLayout 
	    android:layout_height="wrap_content"
		android:orientation="vertical" 
		android:layout_width="match_parent"
		android:gravity="center">
		
		<TextView android:id="@+id/textView1" 
			android:textSize="30dip"
			android:layout_height="60dip" 
			android:gravity="center"
			android:textColor="#ffffff" 
			android:background="@color/cyan"
			android:layout_width="fill_parent" 
			android:text="必备单词"/>
			
		<LinearLayout android:layout_height="wrap_content"
			android:orientation="horizontal" 
			android:layout_width="match_parent"
			android:gravity="center">
			
			<TextView android:id="@+id/textView2"
			    android:text="课程重点："	
				android:textColor="#000000" 
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"/>
				
			<Spinner android:id="@+id/spinner1"
			    android:layout_height="wrap_content"
				android:entries="@array/bibei"
				android:prompt="@string/srp_name"
				android:layout_width="200dip" />
				
		</LinearLayout>
		
		<LinearLayout 
		    android:layout_height="120dip"
			android:orientation="vertical" 
			android:gravity="right"
			android:layout_width="280dip">
			
			<RelativeLayout 
			    android:layout_height="120dip"
				android:orientation="horizontal" 	
				android:layout_width="280dip">
				
				<TextView android:id="@+id/wordView2"	
					android:textColor="#000000" 
					android:textSize="30dip"
					android:background="@drawable/bg_border"
					android:gravity="center|center_vertical"
					android:layout_width="280dip"
					android:layout_height="match_parent"/>
					
				<TextView android:id="@+id/sumView2"
				    android:text="0/0" 	
					android:textColor="#000000" 
					android:layout_alignParentRight="true"
					android:layout_width="wrap_content"
					android:layout_height="wrap_content"/>
					
				<TextView android:id="@+id/textView7"
				    android:layout_width="wrap_content"
					android:visibility="gone" 
					android:layout_marginLeft="10dip"
					android:textColor="#ffffff" 	
					android:layout_height="wrap_content" 
					android:text=""/>
					
				<ImageView android:id="@+id/imageView1"
				    android:layout_height="wrap_content" 	 
					android:layout_alignParentLeft="true"
					android:layout_marginLeft="3dip"
					android:layout_marginTop="3dip"
					android:layout_width="wrap_content" 
					android:src="@drawable/qqqw"/>
					
				<LinearLayout 
			        android:layout_height="wrap_content"
				    android:orientation="vertical" 
				    android:layout_alignParentBottom="true"	
				    android:gravity="center"
				    android:layout_width="280dip">
				    
				    <ImageView android:id="@+id/imageShengyin"
				    android:layout_height="wrap_content" 	 
					android:layout_alignParentLeft="true"
					android:layout_width="wrap_content" 
					android:src="@drawable/suona"/>
				    
				</LinearLayout>
				
			</RelativeLayout>
			
		</LinearLayout>
		
		<LinearLayout 
		    android:layout_height="120dip"
			android:orientation="horizontal" 
			android:gravity="center"
			android:layout_width="280dip">
			
			<TextView android:id="@+id/enpView2" 
			    android:layout_height="match_parent" 	
				android:textColor="#000000" 
				android:textSize="20dip"
				android:background="@drawable/bg_border"
				android:gravity="center|center_vertical"
				android:text="" 
				android:layout_width="280dip"/>
				
			<TextView android:id="@+id/textID"
			    android:layout_width="wrap_content"
				android:visibility="gone" 
				android:layout_marginLeft="10dip"
				android:textColor="#ffffff" 
				android:layout_height="wrap_content" 
				android:text=""/>
				
		</LinearLayout>
		
		<SeekBar android:id="@+id/seekbar"
			android:layout_width="280dip" 
			android:layout_height="wrap_content" />
			
		<LinearLayout 
		    android:layout_height="wrap_content"
			android:orientation="horizontal" 
			android:gravity="center"
			android:layout_width="match_parent">
			
			<Button android:id="@+id/liubutton1"
			    android:layout_height="wrap_content" 
				android:text="自动浏览"
				android:layout_width="wrap_content" />
				
			<Button android:id="@+id/shangbutton1"
			    android:layout_height="wrap_content" 
				android:text="上一页"
				android:layout_width="wrap_content" />
				
			<Button android:id="@+id/xiabutton1"
			    android:layout_height="wrap_content" 
				android:text="下一页"
				android:layout_width="wrap_content" />
		</LinearLayout>
	
	</LinearLayout>
	
	<LinearLayout 
	    android:layout_height="wrap_content"
		android:orientation="vertical" 
		android:layout_width="match_parent">
		
		<Button android:id="@+id/button1"
		    android:layout_height="wrap_content" 
			android:text="返回"
			android:layout_width="80dip" />

	</LinearLayout>
	
	<LinearLayout 
	    android:layout_height="wrap_content"
		android:orientation="horizontal" 
		android:layout_alignParentBottom="true"
		android:layout_width="match_parent">
		
		<TextView android:id="@+id/kabView2"
		    android:text="卡片模式" 			
			android:textColor="#ffffff" 
			android:background="#2F4F4F"
			android:gravity="center|center_vertical"
			android:layout_width="160dip"
			android:layout_height="40dip" />
			
		<TextView android:id="@+id/lieView2"
		    android:text="列表模式" 			
			android:textColor="#ffffff" 
			android:background="#2F4F4F"
			android:gravity="center|center_vertical"
			android:layout_width="160dip"
			android:layout_height="40dip"/>
		
	</LinearLayout>
	
</RelativeLayout>
```

 有些命名什么的不规范，看得懂就行，界面也不好看，不过那是样式的问题了

灵活用LinearLayout和RelativeLayout嵌套使用，足可以应付你的界面了。比如你要一控件在你想要的

任何地方，用RelativeLayout的android:layout\_marginTop（距离顶部多少多少）是可以，但界面不可

能就一个控件，有些控件不需要这样，当然就RelativeLayout绝对可以但很麻烦……所以我们可以给一

个LinearLayout让这个控件跟其他区域分清楚点，再在里面用RelativeLayout来布局，这样分块就会很

清晰。

好了，原先我的界面布出来了，在虚拟机里运行也很正常，但当我下到自己的手机安装的时候，哎呀！

那界面真是惨不忍睹啊，有些控件正常显示，有些被缩得跟头发一样（夸张了点），反正界面都变形了

……可以是做为Android新手的我不知道啊，后来问过前辈才知道……我用了

android:layout\_height="50px"  ，用px来定大小的话很容易出错，因为它是根据像素来的，比如你画

160px的线在480\*800像素的手机上就是1/3长，那么在320\*480的手机上就是一半长了，如果这样的话，

就很尴尬了……但Android里有一个dip，用这个的话，在不同的设备会有不同的效果，我把它理解成是

根据比例来的（个人这样理解）；就说我觉得比较容易理解的吧反正这样用没错啦：dip把手机统一假设

为是320\*480像素的，1dip=1px,如果把宽设为160dip那么就是频幕宽的一半，不管是在320\*480像素的，

还是480\*800像素的等等，它都是频幕宽的一半，高也是这样啊。

可能你觉得我的废话比较多，但是看完一件事件的经过，会让你可以举一反三（也是在告诫我自己）。