我们平常可以直接在xml里设置margin,如:

<ImageViewandroid:layout_margin="5dip"android:src="@drawable/image"/>

但是有些情况下,需要在java代码里来写,可是View本身没有setMargin方法,怎么办呢?

通过查阅android api,我们发现android.view.ViewGroup.MarginLayoutParams有个方法setMargins(left, top, right, bottom).

其直接的子类有: FrameLayout.LayoutParams,

        LinearLayout.LayoutParams,

        RelativeLayout.LayoutParams.

使用方法:

LinearLayout.LayoutParamslp= newLinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(10,20,30,40);
imageView.setLayoutParams(lp);

更多相关文章

  1. Android之日期时间选择器使用方法
  2. Android(安卓)启动浏览器的方法
  3. Android蓝牙通信代码
  4. 对比onSaveInstanceState和onRestoreInstanceState
  5. 去掉Android(安卓)app上的Title Bar
  6. 详解 Android(安卓)的 Activity 组件
  7. Android使用SQLite数据库的示例
  8. 浅谈Java中Collections.sort对List排序的两种方法
  9. Python list sort方法的具体使用

随机推荐

  1. android中The connection to adb is down
  2. Android将ListView逆时针旋转90°实现横
  3. android开机自启动apk
  4. [Android]笔记7-RelativeLayout布局
  5. Android沉浸式状态栏
  6. Android的四种基本布局
  7. 由Android(安卓)65K方法数限制引发的思考
  8. Android studio 打不开官方虚拟机 100%成
  9. Android中 ScrollView(ListView)中嵌套List
  10. Android之toolbar的使用