TextView
- TextView设置drawable代码方式:
TextView textView = new TextView(context);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(layoutParams);
Drawable drawable = ResourceUtils.getDrawable(R.drawable.ic_name);
int px14 = ConvertUtils.dp2px(14);
drawable.setBounds(0, 0, px14, px14);
textView.setCompoundDrawables(drawable, null, null, null);
int px = ConvertUtils.dp2px(10);
textView.setCompoundDrawablePadding(px);
textView.setText("我是标题");
drawable.setBounds(left, top, right, bottom)
//drawable会在这里用指定的位置绘制出来
textView.setCompoundDrawables(left, top, right, bottom)
//设置绘制对象的方位
Margin
- 设置View的边距(Margin)
- 定义在xml中通过
findViewById
获取的控件
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) textview.getLayoutParams();
lp.leftMargin = 0;
textview.setLayoutParams(lp);
- 通过new出来的控件
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10,10,10,10);//4个参数按顺序分别是左上右下
textview.setLayoutParams(layoutParams);
用@IntDef代替Enum
使用要点:
- 定义常量;
//布局方式 public static final int HORIZONTAL = 0; public static final int HORIZONTAL_CENTER = 1; public static final int VERTICAL = 2;
- 用
@IntDef
包裹常量;用@Retention
定义一个策略
@IntDef({IFormModule.HORIZONTAL, IFormModule.HORIZONTAL_CENTER, IFormModule.VERTICAL}) @Retention(RetentionPolicy.SOURCE) public @interface Orientation {}
需要注意定义的interface前面需要用
@
的标注@interface
- 在定义的方法上使用
策略
public void setOrientation(@Orientation int orientation) { System.out.println("引用布局的参数:方向:" + orientation); } imgPickerItem.setOrientation(IFormModule.VERTICAL);