// 相对于给定ID控件
android:layout_above 将该控件的底部置于给定ID的控件之上;
android:layout_below 将该控件的底部置于给定ID的控件之下;
android:layout_toLeftOf
android:layout_toRightOf
android:layout_alignBaseline
android:layout_alignTop
android:layout_alignBottom
android:layout_alignLeft
android:layout_alignRight
// 相对于父组件
android:layout_alignParentTop
android:layout_alignParentBottom 如果为true,将该控件的底部与其父控件的底部对齐;
android:layout_alignParentLeft
android:layout_alignParentRight
// 居中
android:layout_centerHorizontal 如果为true,将该控件的置于水平居中;
android:layout_centerVertical
android:layout_centerInParent
// 指定移动像素
android:layout_marginTop
android:layout_marginBottom 下偏移的值;
android:layout_marginLeft 左偏移的值;
android:layout_marginRight 右偏移的值;
通俗的说,inflate就相当于将一个xml中定义的布局找出来.
因为在一个Activity里如果直接用findViewById()的话,对应的是setConentView()的那个layout里的组件.
因此如果你的Activity里如果用到别的layout,比如对话框上的layout,你还要设置对话框上的layout里的组件(像图片ImageView,文字TextView)上的内容,你就必须用inflate()先将对话框上的layout找出来,然后再用这个layout对象去找到它上面的组件,如:
Viewview=View.inflate(this,R.layout.dialog_layout,null);
TextViewdialogTV=(TextView)view.findViewById(R.id.dialog_tv);
dialogTV.setText("abcd");
如果组件R.id.dialog_tv是对话框上的组件,而你直接用this.findViewById(R.id.dialog_tv)肯定会报错.
三种方式可以生成LayoutInflater:
LayoutInflaterinflater=LayoutInflater.from(this);
LayoutInflaterinflater=getLayoutInflater();
LayoutInflaterinflater=(LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
然后调用inflate方法将xml布局文件转成View
publicViewinflate(intresource,ViewGrouproot,booleanattachToRoot)
在View类中,也有inflate方法
publicstaticViewinflate(Contextcontext,intresource,ViewGrouproot)
本文深入探讨了Android开发中使用布局、视图管理、布局约束和视图定位的相关概念与实践,包括如何利用XML布局文件进行界面设计,以及如何通过Java代码动态调整视图位置和大小。此外,还介绍了如何使用LayoutInflater和inflate方法将XML布局转换为View对象,并解释了在不同场景下如何灵活运用这些工具来优化UI设计。
643

被折叠的 条评论
为什么被折叠?



