Android 常用小知识

  • TextView 添加下划线
        TextView textView = findViewById(R.id.textView);
        String text = "这是一段测试文字";
        //方法一
        textView.setText(Html.fromHtml(String.format("%s", text)));
        //方法二
        //这是一段测试文字}
        textView.setText(R.string.example);
        //方法三
        textView.getPaint().setFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
        //方法四
        SpannableString ss = new SpannableString(text);
        ss.setSpan(new UnderlineSpan(), 0, text.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        textView.setText(ss);
  • Webview 加载html
        //加载url
        webView.loadUrl("http://www.baidu.com");
        //加载asset目录下html文件
        webView.loadUrl("file:///android_asset/index.html");
        //加载 手机内部存储或sd卡 内的html文件
        webView.loadUrl("file:///storage/emulated/0/index.html");
  • ListView,RecyclerView等设置具有padding的分割线
    设置分割线,我们只需要提供DividerDrawable即可,示例代码如下
        //ListView设置分割线
        mListView.setDivider(drawable);
        //RecyclerView设置分割线
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        dividerItemDecoration.setDrawable(drawable);
        mRecyclerView.addItemDecoration(dividerItemDecoration);

方式一:使用xml,drawable/divider.xml


    
        
        
    

方式二:使用代码:

        ShapeDrawable shapeDrawable = new ShapeDrawable();
        shapeDrawable.setColorFilter(color, mode);
        shapeDrawable.setIntrinsicHeight(height);
        InsetDrawable drawable = new InsetDrawable(shapeDrawable, insetLeft, insetTop, insetRight, insetBottom);
  • TabLayout常见用法
  1. 设置字体大小
    定义style,如下:

然后在xml中使用,如下:


2.设置Tab间分割线,方法如下

View root = tabLayout.getChildAt(0);
if (root instanceof LinearLayout) {
      ((LinearLayout) root).setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
      GradientDrawable drawable = new GradientDrawable();
      drawable.setColor(Color.BLACK);
      drawable.setSize(2, 1);
      ((LinearLayout) root).setDividerDrawable(drawable);
}
  • 设置AppCompatRatingBar仅仅显示,不可交互
android:isIndicator="true"
  • CardView设置透明背景的问题
    方法一:XML设置cardBackgroundColor属性为TRANSPARENT
    方法二:代码设置setCardBackgroundColor(Color.TRANSPARENT)
    方法三:代码设置setBackgroundColor(Color.TRANSPARENT)
    注意:CardView在XML设置background是无效的,原因很简单,看源代码即可:
//CardView的构造函数中会调用以下代码
TypedArray a = context.obtainStyledAttributes(attrs, styleable.CardView, defStyleAttr, style.CardView);
ColorStateList backgroundColor;
if (a.hasValue(styleable.CardView_cardBackgroundColor)) {
    backgroundColor = a.getColorStateList(styleable.CardView_cardBackgroundColor);
} else {
    TypedArray aa = this.getContext().obtainStyledAttributes(COLOR_BACKGROUND_ATTR);
    int themeColorBackground = aa.getColor(0, 0);
    aa.recycle();
    float[] hsv = new float[3];
    Color.colorToHSV(themeColorBackground, hsv);
    backgroundColor = ColorStateList.valueOf(hsv[2] > 0.5F ? this.getResources().getColor(color.cardview_light_background) : this.getResources().getColor(color.cardview_dark_background));
}
///省略...
IMPL.initialize(this.mCardViewDelegate, context, backgroundColor, radius, elevation, maxElevation);
//IMPL.initialize会调用mCardViewDelegate.setCardBackground方法
//而mCardViewDelegate.setCardBackground最终会调用CardView.this.setBackgroundDrawable(drawable)方法

由以上分析可知,在XML通过background属性设置的背景色最终会被冲掉,所以CardView在XML里设置background是无效的

  • 解决发送邮件时换行符\n不生效的问题
//解决办法:设置Type为text/html,换行符使用
try { Intent intent = new Intent(Intent.ACTION_SENDTO); intent.setType("text/html") .setData(Uri.parse("mailto:"))// only email apps should handle this .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP) .putExtra(Intent.EXTRA_EMAIL, addresses) .putExtra(Intent.EXTRA_SUBJECT, "SUBJECT") .putExtra(Intent.EXTRA_TEXT, "<\\br><\\br>text<\\br>"); if (intent.resolveActivity(context.getPackageManager()) != null) { context.startActivity(intent); } } catch (Exception e) { }

你可能感兴趣的:(Android 常用小知识)