Detailed explanation of the use of attrs.xml file in Android

1. The role of attrs.xml

Controls have many attributes, such as android:id, android:layout_width, android:layout_height, etc., but these attributes are all built-in attributes of the system. Using the attrs.xml file, you can define your own attributes . I will write some small demos below, which are easier to understand

2. In the values ​​folder, create a new attrs.xml file

The content is as follows:


<?xml version="1.0" encoding="utf-8"?>
<declare-styleable name="MyView">
        <attr name="textColor" format="color" />
        <attr name="textSize" format="dimension" />
 </declare-styleable>

in,

<declare-styleable name="MyView">

Indicates that the style name is MyView, and the following contains two custom attributes textColor and textSize, where textColor is an attribute of the color (color) class, and textSize is an attribute of the dimension (dimension) class

3. Customize MyView

public class MyView extends View {

    private Paint mPaint;
    private static final String mString = "Welcome to BaiYe's blog";

    public MyView(Context context) {
        this(context,null);
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyView);
        int textColor = a.getColor(R.styleable.MyView_textColor, 0XFFFFFFFF);
        float textSize = a.getDimension(R.styleable.MyView_textSize, 36);

        mPaint.setTextSize(textSize);
        mPaint.setColor(textColor);
        a.recycle();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 设置填充
        mPaint.setStyle(Paint.Style.FILL);
        // 画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标
//        mPaint.setColor(Color.BLACK);
        canvas.drawRect(new Rect(10, 10, 100, 100), mPaint);
        // 绘制文字
        canvas.drawText(mString, 60, 410, mPaint);
    }
}
  • First got the TypedArray variable from R.styleable.CustomView
  • Then use getColor(), getDimension() and other methods to obtain the corresponding attribute value. The attribute format is "style name_attribute name", and the parameters behind the attribute are the default values.
  • Once the property values ​​are obtained, they can be applied.
  • The recycle() method is used to return signals to resources (do not understand what it means)

4. xml content

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:test="http://schemas.android.com/apk/res-auto"//一定记得添加前缀 
    android:id="@+id/activity_attrs_actiity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.lizi.newset.CustomView.attrs.AttrsActivity">

    <com.lizi.newset.CustomView.attrs.MyView
        android:id="@+id/myView"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        test:textSize="50px"
        test:textColor="#ff00ff"/>
    />
</RelativeLayout>

xmlns:test=”http://schemas.android.com/apk/res-auto” must be added, and then the attributes can be customized in xml

5. Custom Properties

The format is as above, where the "xmlns:test" colon is followed by the tag name, when used below (only available for the current file)

<TextView  test:属性名/>

5.1 reference: refer to a resource ID


<declare-styleable name = "名称">
      <attr name = "background" format = "reference" />
</declare-styleable>

eg:

<ImageView
     android:layout_width = "42dip"
     android:layout_height = "42dip"
     android:background = "@drawable/图片ID"                     />

5.2 color: color value


<declare-styleable name = "名称">
       <attr name = "textColor" format = "color" />
</declare-styleable>

eg:


 <TextView
     android:layout_width = "42dip"
     android:layout_height = "42dip"
     android:textColor = "#00FF00"
                     />

5.3 boolean: Boolean value


<declare-styleable name = "名称">
      <attr name = "focusable" format = "boolean" />
</declare-styleable>

eg:


<Button
    android:layout_width = "42dip"
    android:layout_height = "42dip"
    android:focusable = "true"/>

5.4 dimension: dimension value


<declare-styleable name = "名称">
     <attr name = "layout_width" format = "dimension" />
</declare-styleable>

eg:

<com.lizi.newset.CustomView.attrs.MyView
        android:id="@+id/myView"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        test:textSize="50px"
        test:textColor="#ff00ff"/>

5.5 float: floating point value


<declare-styleable name = "AlphaAnimation">
      <attr name = "fromAlpha" format = "float" />
      <attr name = "toAlpha" format = "float" />
</declare-styleable>

eg:


<alpha
       android:fromAlpha = "1.0"
       android:toAlpha = "0.7"
/>

5.6 string: String


<declare-styleable name = "MapView">
     <attr name = "apiKey" format = "string" />
</declare-styleable>

eg:


<com.google.android.maps.MapView
         android:layout_width = "fill_parent"         android:layout_height = "fill_parent"
         android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"
                    />

5.7 integer: integer value || fraction: percentage


<declare-styleable name = "AnimatedRotateDrawable">
      <attr name = "visible" />
      <attr name = "frameDuration" format="integer" />
      <attr name = "framesCount" format="integer" />
      <attr name = "pivotX"  format = "fraction"/>
      <attr name = "pivotY"  format = "fraction"/>
      <attr name = "drawable" />
</declare-styleable>

eg:


<animated-rotate
      xmlns:android = "http://schemas.android.com/apk/res/android"  
      android:drawable = "@drawable/图片ID"  
      android:pivotX = "50%"  
      android:pivotY = "50%"  
      android:framesCount = "12"  
      android:frameDuration = "100"
  />

5.8 enum: enumeration value


<declare-styleable name="名称">
                   <attr name="orientation">
                          <enum name="horizontal" value="0" />
                          <enum name="vertical" value="1" />
                   </attr>            
</declare-styleable>

eg:


<LinearLayout
                    xmlns:android = "http://schemas.android.com/apk/res/android"
                    android:orientation = "vertical"
                    android:layout_width = "fill_parent"
                    android:layout_height = "fill_parent"
                    >
</LinearLayout>

5.9 flag bitwise OR


 <declare-styleable name="名称">
                    <attr name="windowSoftInputMode">
                            <flag name = "stateUnspecified" value = "0" />
                            <flag name = "stateUnchanged" value = "1" />
                            <flag name = "stateHidden" value = "2" />
                            <flag name = "stateAlwaysHidden" value = "3" />
                            <flag name = "stateVisible" value = "4" />
                            <flag name = "stateAlwaysVisible" value = "5" />
                            <flag name = "adjustUnspecified" value = "0x00" />
                            <flag name = "adjustResize" value = "0x10" />
                            <flag name = "adjustPan" value = "0x20" />
                            <flag name = "adjustNothing" value = "0x30" />
                     </attr>         
</declare-styleable>

eg:


<activity
      android:name = ".StyleAndThemeActivity"
      android:label = "@string/app_name"
      android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">
      <intent-filter>
            <action android:name = "android.intent.action.MAIN" />
            <category android:name = "android.intent.category.LAUNCHER" />
      </intent-filter>
</activity>

6. You can define multiple types of values ​​at the same time when defining attributes


<declare-styleable name = "名称">
      <attr name = "background" format = "reference|color" />
</declare-styleable>

eg:


<ImageView
        android:layout_width = "42dip"
        android:layout_height = "42dip"
        android:background = "@drawable/图片ID|#00FF00"
        />

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325556861&siteId=291194637