LayoutParams的详解

来源:互联网 发布:代上淘宝活动 编辑:程序博客网 时间:2024/06/15 05:37

简述:

LayoutParams继承于Android.View.ViewGroup.LayoutParams相当于一个Layout的信息包,它封装了Layout的位置、高、宽等信息。假设在屏幕上一块区域是由一个Layout占领的,如果将一个View添加到一个Layout中,最好告诉Layout用户期望的布局方式,也就是将一个认可的layoutParams传递进去。

通俗地讲(这里借鉴了网上的一种说法),LayoutParams类是用于child view(子视图)向parent view(父视图)传达自己的意愿的一个东西(孩子想变成什么样向其父亲说明)。举个栗子,子视图和父视图分别可以简单理解成一个LinearLayout 和该LinearLayout里边的一个 TextView 的关系, TextView 就算LinearLayout的子视图 child view。需要注意的是LayoutParams只是ViewGroup的一个内部类这里边这个也就是ViewGroup里边这个LayoutParams类是base class基类实际上每个不同的ViewGroup都有自己的LayoutParams子类。比如AbsListView.LayoutParams, AbsoluteLayout.LayoutParams, Gallery.LayoutParams, ViewGroup.MarginLayoutParams, WindowManager.LayoutParams等。

ViewGroup.LayoutParams类只能简单的设置高height以及宽width两个基本的属性,宽和高都可以设置成三种值:

       1,一个确定的值;

       2,MATCH_PARENT;

       3,WRAP_CONTENT。

ViewGroup.MarginLayoutParams类是ViewGroup.LayoutParams的子类,顾名思义,它只能设置child View的margin属性信息。

     1、利用setMargins(left,top,right,bottom);

     2、利用MarginLayoutParams对象params方法单独设置.topMargin

基本使用:

    常用子类的简单使用:LinearLayout.LayoutParams、RelativeLayout.Params以及WindowManager.Params。

一、LinearLayout.LayoutParams

LinearLayout.LayoutParams的继承关系—>ViewGroup.MarginLayoutParams—>ViewGroup.LayoutParams.从继承关系来看LinearLayout.LayoutParams最少已经可以支持动态设置高度、宽度以及margin属性。

LinearLayout.Params本身自己的属性:gravity和weight属性

基本的使用

1、创建xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.lzy.layoutparamsdemo.MainActivity" >    <ImageView        android:id="@+id/img"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@drawable/ic_launcher" /></LinearLayout>

2.在MainActivity中动态设置ImageView的属性

public class MainActivity extends Activity {private ImageView img;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initViews();}/** * @方法描述: * @author lizhenya */private void initViews() {img = (ImageView) findViewById(R.id.img);LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(img.getLayoutParams());/** * 高度和宽度是从ViewGroup.MarginLayoutParams继承而来,ViewGroup.MarginLayoutParams的高度和宽度又是 * 从ViewGroup.Params继承而来,是基本的属性. */params.height = ViewGroup.LayoutParams.MATCH_PARENT;params.width = ViewGroup.LayoutParams.MATCH_PARENT;/** * margin属性是从ViewGroup.MarginLayoutParams继承而来。 */params.setMargins(200, 200, 0, 0);//LinearLayout.LayoutParams自身的属性params.gravity =  Gravity.CENTER;params.weight =  (float) 1.0;img.setLayoutParams(params);}}

                

                                                                            效果图前后的对比                    

2 0
原创粉丝点击