常见Layout的LayoutParams总结

本文详细解析了Android中不同布局类型的LayoutParams属性,包括FrameLayout、LinearLayout、RelativeLayout等,阐述了各属性的作用及相互之间的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

LayoutParams

java.lang.Object

<-- android.view.ViewGroup.LayoutParams

 

android:layout_height        

Specifies the basic height of the view. 
android:layout_width         

Specifies the basic width of the view.

 


MarginLayoutParams

java.lang.Object

<-- android.view.ViewGroup.LayoutParams

<-- android.view.ViewGroup.MarginLayoutParams

 

android:layout_marginBottom        

Specifies extra space on the bottom side of this view.
android:layout_marginLeft             

Specifies extra space on the left side of this view.
android:layout_marginRight           

Specifies extra space on the right side of this view.
android:layout_marginTop              

Specifies extra space on the top side of this view.

 


FrameLayout

java.lang.Object

<-- android.view.ViewGroup.LayoutParams

<-- android.view.ViewGroup.MarginLayoutParams

<-- android.widget.FrameLayout.LayoutParams

 

android:layout_gravity            

Standard gravity constant that a child can supply to its parent

 

注意区别android:gravity, 这个属性是android.view.Gravity,

FrameLayout.LayoutParams, LinearLayout.LayoutParams和各种常见的View都有 android:gravity ;注意RelativeLayout.LayoutParams没有这个属性。

 

 

LinearLayout

java.lang.Object

<-- android.view.ViewGroup.LayoutParams

<-- android.view.ViewGroup.MarginLayoutParams

<-- android.widget.LinearLayout.LayoutParams

 

android:layout_gravity          

Standard gravity constant that a child can supply to its parent

android:layout_weight            

 

 

RelativeLayout

java.lang.Object

<-- android.view.ViewGroup.LayoutParams

<-- android.view.ViewGroup.MarginLayoutParams

<-- android.widget.RelativeLayout.LayoutParams

 

android:layout_above                            

Positions the bottom edge of this view above the given anchor view ID.
android:layout_alignBaseline                 

Positions the baseline of this view on the baseline of the given anchor view ID.
android:layout_alignBottom                   

Makes the bottom edge of this view match the bottom edge of the given anchor view ID.
android:layout_alignLeft                        

Makes the left edge of this view match the left edge of the given anchor view ID.
android:layout_alignParentBottom          

If true, makes the bottom edge of this view match the bottom edge of the parent.
android:layout_alignParentLeft               

If true, makes the left edge of this view match the left edge of the parent.
android:layout_alignParentRight             

If true, makes the right edge of this view match the right edge of the parent.
android:layout_alignParentTop               

If true, makes the top edge of this view match the top edge of the parent.
android:layout_alignRight                       

Makes the right edge of this view match the right edge of the given anchor view ID.
android:layout_alignTop                         

Makes the top edge of this view match the top edge of the given anchor view ID.
android:layout_alignWithParentIfMissing      

If set to true, the parent will be used as the anchor when the anchor cannot be be found for layout_toLeftOf, layout_toRightOf, etc.
android:layout_below                            

Positions the top edge of this view below the given anchor view ID.
android:layout_centerHorizontal           

If true, centers this child horizontally within its parent.
android:layout_centerInParent              

If true, centers this child horizontally and vertically within its parent.
android:layout_centerVertical               

If true, centers this child vertically within its parent.
android:layout_toLeftOf                         

Positions the right edge of this view to the left of the given anchor view ID.
android:layout_toRightOf                      

Positions the left edge of this view to the right of the given anchor view ID.

 

 

TableLayout

java.lang.Object

<-- android.view.ViewGroup.LayoutParams

<-- android.view.ViewGroup.MarginLayoutParams

<-- android.widget.LinearLayout.LayoutParams

<-- android.widget.TableLayout.LayoutParams

 

 

AbsoluteLayout

java.lang.Object

<-- android.view.ViewGroup.LayoutParams
<-- android.widget.AbsoluteLayout.LayoutParams

 

java.lang.Object 

<-- android.R.styleable

<-- public static final int[] AbsoluteLayout_Layout

 

android:layout_x
android:layout_y

 

Note: AbsoluteLayout is deprecated. Use other layouts instead.

 

 

 

总结

 

1. 界面的原点(0, 0)是除去status bar和title bar之后剩下的区域。 如果使用了全屏,不显示状态栏,不显示标题栏这样的主题后,区域的原点位置会相应改变。

 

2. FrameLayout的widget中使用类似android:layout_marginLeft="65px"这样的属性,一定要加上android:layout_gravity,否则margin无效。还要注意FrameLayout的android:layout_width和android:layout_height对layout_gravity的影响。

 

3. 使用布局属性一定要分清谁是parent,parent用的是什么layout,layout_width和layout_height的值。

 

4. 不同的布局属性也可以实现相同的功能。例如layout_gravity="center"和android:layout_centerInParent ="true"。

 

5. Eclipse的Android开发工具插件ADT里面有一个所见即所得的开发UI的功能。利用Graphical Layout可以预览的效果。但是,有时会遇到以下问题:

error!
UnsupportedOperationException: null

一般来说,这是因为所选择的Android版本不支持布局设置或者Android SDK不能很好的支持该layout的显示。可以尝试换其他Android版本或者看看该版本的SDK有没有更新。

 

 

 

for (int i = 0;i<4;i++){ if (i % 2 == 0){ currentRow = new LinearLayout(this); currentRow.setOrientation(LinearLayout.HORIZONTAL); LinearLayout.LayoutParams rowParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT ); rowParams.bottomMargin = 16; // 设置行与行之间的间距 container.addView(currentRow, rowParams); } // 创建一个垂直的 LinearLayout 用于包含图片和文字 LinearLayout itemLayout = new LinearLayout(this); itemLayout.setOrientation(LinearLayout.VERTICAL); // 创建 ImageView 并设置图片 ImageView imageView = new ImageView(this); imageView.setImageURI(imageUris2[i]); LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams( 600, // 图片宽度 400 // 图片高度 ); imageView.setLayoutParams(imageParams); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); itemLayout.addView(imageView); // 创建 TextView 并设置文字 TextView textView = new TextView(this); textView.setText(texts2[i]); LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT ); textParams.gravity = android.view.Gravity.CENTER_HORIZONTAL; textParams.topMargin = 8; // 设置文字与图片的间距 textView.setLayoutParams(textParams); itemLayout.addView(textView); // 将组合视图添加到当前行 LinearLayout.LayoutParams itemParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT ); itemParams.rightMargin = 66; // 设置元素之间的间距 itemParams.bottomMargin = 66; currentRow.addView(itemLayout, itemParams); } 怎么设置图片圆角
03-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值