自己测试的代码:
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="225px"
android:layout_height="wrap_content"
android:text="BtN123456789"
android:gravity="right"
android:layout_gravity="right" />
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Btn2" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right" >
<Button
android:id="@+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="100px"
android:layout_height="wrap_content"
android:paddingTop="50px"
android:paddingLeft="50px"
android:text="Btn4"
android:gravity="left" />
<Button
android:id="@+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BTN5" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
java代码:
public class MainActivity extends Activity {
Button btn1,btn2,btn4,btn5;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
///-------------------------------------------
btn1=(Button)findViewById(R.id.button1);
btn2=(Button)findViewById(R.id.button2);
btn4=(Button)findViewById(R.id.button4);
btn5=(Button)findViewById(R.id.button5);
new Handler().postDelayed(new Runnable(){ ///因刚onCreate,图像还没有渲染好,如立即读取,则返回0
@Override
public void run(){
Log.i("sno","testLayout--------btnX"+" "+btn5.getWidth()+"+"+btn5.getLeft()+" "+btn5.getMeasuredWidth()+"??"+" "+"padding"+btn5.getPaddingLeft());
}
}, 1000);
}
}
-----------------------自己的总结:
--------------关于布局时的尺寸问题:
1,单位 (来源于网络)
px: 像素 -----单位尺寸里的像素点
dp: 密度 ----- 一个基于density的抽象单位,如果一个160dpi的屏幕,1dp=1px
dip:等同于dp
sp: 同dp相似,但还会根据用户的字体大小偏好来缩放,常用来作为字体大小的单位。即为像素与像素密度的换算关系:
pixs =dips * (densityDpi/160)
dips=(pixs*160)/densityDpi
Android的屏幕密度是以160为基准的,屏幕密度(density)为160时,是将一英寸分为160份, 每一份是1像素;
同理如果屏幕密度(density)为240时,是将一英寸分为240份,,每一份是1像素,
所以近来的新的sdk为了适配不同的屏幕分辨率的机型,已经陆续取消采用像素px作为布局单位这主要是针对不同设备而言的。
因为px不管在什么样的设备上都是那样长,但是dip会根据设备变化;
当屏幕density=240时使用hdpi标签的资源;
当屏幕density=160时,使用mdpi标签的资源 ;
当屏幕density=120时,使用ldpi标签的资源
单位代码换算
public static int dip2px(Context context, float dipValue){
final float scale = context.getResources().getDisplayMetrics().density;
return (int)(dipValue * scale + 0.5f);
}
public static int px2dip(Context context, float pxValue){
final float scale = context.getResource().getDisplayMetrics().density;
return (int)(pxValue / scale + 0.5f);
}
public static int dip2px(Context context, float dipValue){
final float scale = context.getResources().getDisplayMetrics().density;
return (int)(dipValue * scale + 0.5f);
}
public static int px2dip(Context context, float pxValue){
final float scale = context.getResource().getDisplayMetrics().density;
return (int)(pxValue / scale + 0.5f);
}
2,几个设置获取的函数:
2.1 总宽高:
getMeasuredWidth() and getMeasuredHeight().是当前控件在父控件中占据的总的宽高,在布局的时候使用,单位是像素,
(很多时候等于getWidth()),例如自定义一个viewgroup后,在onLayout时需要:
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childLeft = 0;
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != View.GONE) {
final int childWidth = child.getMeasuredWidth(); ///这里是水平排列.
child.layout(childLeft, 0, childLeft + childWidth,
child.getMeasuredHeight());
childLeft += childWidth;
}
}
}
2.2 控件本身宽高:
getWidth()和getHeight()是获取控件本身的宽高(单位是像素点),不要在onCreate中获取,因为还没有渲染好,返回的结果是0, 可以
handler.postdelay来做.见举例.
2.3 控件边界:
getLeft/Right/Bottom/Top控件的边缘和父layout之间的距离,默认左边的是0,单位是像素.
2.4 控件中内容的矩形边界
padding的中文是填充意思,是Button上内容的填充.比如一个Button,Button上面有文字,文字放到Button控件中一个矩形里的,矩形中放文字内容.padding的意思是这个矩形的四个边和Button控件的边之间的距离,默认是10个像素,
getPaddingLeft(), getPaddingTop(), getPaddingRight(), getPaddingBottom(), getPaddingStart(), getPaddingEnd().就是获取这个矩形边和Button边的距离.
设置:
xml方式:android:paddingTop="50px"
android:paddingLeft="50px"
代码方式: setPadding(int, int, int, int) or setPaddingRelative(int, int, int, int)
2.5 控件中矩形内内容对齐方式:
android:gravity 表示Button上矩形内的文字的对齐方式,是指如left,center,right等,默认是中心对齐.
android:layout_gravity 表示Button本身在这个layou中的方式,如left,center,right等,默认是中心对齐. 暂时没有找到怎么在java中设置.
(欢迎下载使用我的播放器:http://static.apk.hiapk.com/html/2013/05/1444487.html)