阅读 74

Android开发要点杂记

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)
  1. 定义在xml中通过findViewById获取的控件
    LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) textview.getLayoutParams();  
    lp.leftMargin = 0;  
    textview.setLayoutParams(lp);  
复制代码
  1. 通过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

原文地址

使用要点:

  1. 定义常量;
    //布局方式
    public static final int HORIZONTAL = 0;
    public static final int HORIZONTAL_CENTER = 1;
    public static final int VERTICAL = 2;
    复制代码
  2. @IntDef包裹常量;用@Retention定义一个策略
    @IntDef({IFormModule.HORIZONTAL, IFormModule.HORIZONTAL_CENTER, IFormModule.VERTICAL})
    @Retention(RetentionPolicy.SOURCE)
    public @interface Orientation {}
    复制代码

    需要注意定义的interface前面需要用@的标注@interface

  3. 在定义的方法上使用策略
    public void setOrientation(@Orientation int orientation) {
        System.out.println("引用布局的参数:方向:" + orientation);
    }
    
    imgPickerItem.setOrientation(IFormModule.VERTICAL);
    复制代码
文章分类
Android
文章标签