zoukankan      html  css  js  c++  java
  • 自定义适应屏幕大小的TextView

    先来个截图:

    在来看看自定义的TextView:

     1 public class MyTextView extends TextView
     2 {
     3 
     4     public MyTextView(Context context, AttributeSet attrs)
     5     {
     6         super(context, attrs);
     7         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyTextView);
     8         float textSize = a.getDimension(R.styleable.MyTextView_myTextSize, 36);
     9         int size = DisplayUtil.px2dip(textSize);
    10         this.setTextSize(size);
    11         a.recycle();
    12 
    13     }
    14 }

    解释一下代码:

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyTextView); 这个是获取自定义属性的集合。float textSize = a.getDimension(R.styleable.MyTextView_myTextSize, 36); 这个是获取上面集合里面的某一个属性,有一个默认值,防止别人忘写.

    int size = DisplayUtil.px2dip(textSize); 通过工具类把px转换成sp。


    上DisplayUtil的代码:
     1 public class DisplayUtil
     2 {
     3     public static int dip2px(float dipValue)
     4     {
     5         final float scale = MyApplication.context.getResources().getDisplayMetrics().density;
     6         return (int) (dipValue * (scale + 0.5f));
     7     }
     8     public static int px2dip(float pxValue)
     9     {
    10         final float scale = MyApplication.context.getResources().getDisplayMetrics().density;
    11         return (int) (pxValue / (scale + 0.5f));
    12     }
    13 }
    配置自定义属性 需要在values下面创建一个attrs的xml

    1 <?xml version="1.0" encoding="utf-8"?>
    2 <resources>
    3     <declare-styleable name="MyTextView">
    4         <attr name="myTextSize" format="dimension" />
    5     </declare-styleable>
    6 </resources>

    最后mainActivity的布局:

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:mytextview="http://schemas.android.com/apk/res/com.example.mytextview"  
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     tools:context=".MainActivity" >
     7     <com.example.mytextview.MyTextView
     8         android:layout_width="wrap_content"
     9         android:layout_height="wrap_content"
    10         mytextview:myTextSize="45px"
    11         android:text="我是自适应的文字" />
    12     
    13     <TextView android:layout_width="wrap_content"
    14         android:layout_height="wrap_content"
    15         android:textSize="30sp"
    16         android:text="我是普通的文字" />
    17 </LinearLayout>

    第2行很关键的 

    xmlns:mytextview 自己可以随意定义 下面引用要用的
    com.example.mytextview 这个是自已包的名字


  • 相关阅读:
    高效 告别996,开启java高效编程之门 5-4新集合类型MultiSet
    高效 告别996,开启java高效编程之门 5-3不可变集合
    高效 告别996,开启java高效编程之门 5-2实战Optional使用
    高效 告别996,开启java高效编程之门 5-1Guava开场
    高效 告别996,开启java高效编程之门 4-6本节总结
    高效 告别996,开启java高效编程之门 4-5TWR进阶与特殊情况
    高效 告别996,开启java高效编程之门 4-4TWR方式关闭流资源
    高效 告别996,开启java高效编程之门 4-3传统方式关闭流资源
    省市经纬度
    Web移动端常见问题-摘抄
  • 原文地址:https://www.cnblogs.com/androidxiaoyang/p/3014129.html
Copyright © 2011-2022 走看看