Android自定义view

来源:互联网 发布:64码高清网络电视官方 编辑:程序博客网 时间:2024/06/05 21:56

Android自定义view有人会分为自定义view和viewgroup两种,但目前我自己感觉可以分为继承view和继承Android自有的控件这两种,例如我们需要一个全新的控件会继承view或者我们只需要在某些现有的控件基础上修改即可。
这里先说一下自定义view的自定属性
例如我们现在自定义一个MyButton
现在res文件下的values新建一个attr文件,用来声明自己定义的属性

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="MyButton">        <attr name="reference" format="reference"/>        <attr name="dimension" format="dimension"/>    </declare-styleable></resources>

然后在MyButton代码中获取

package yasin.com.diyview;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Bitmap;import android.util.AttributeSet;import android.widget.Button;/** * Created by Yasin on 2016/6/3. */public class MyButton extends Button {    int reference,dp;    public MyButton(Context context) {        super(context);    }    public MyButton(Context context, AttributeSet attrs) {        super(context, attrs);        TypedArray attr = context.obtainStyledAttributes(R.styleable.MyButton);        reference = attr.getResourceId(R.styleable.MyButton_reference,R.mipmap.ic_launcher);        dp = attr.getDimensionPixelOffset(R.styleable.MyButton_dimension,10);        attr.recycle();//释放资源    }    public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }}

在布局文件中使用
这里重点提一下xml的第四行代码,我们需要自己声明一个资源路径,另外一定要自己命名(不能命名为android,这样和系统的冲突),在自定义的view中便可以声明自己的变量了

<?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:myattr="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="yasin.com.diyview.MainActivity">   <yasin.com.diyview.MyButton       android:id="@+id/mybutton"       myattr:dimension="10dp"       myattr:reference="@mipmap/ic_launcher"       android:layout_width="wrap_content"       android:layout_height="wrap_content" /></RelativeLayout>

接下来的在java代码中的使用就很简单了

package yasin.com.diyview;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;public class MainActivity extends AppCompatActivity {    MyButton myButton;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        myButton = (MyButton) findViewById(R.id.mybutton);        Log.e("yasin",""+myButton.dp);    }}

这里对自定义属性的类别进行介绍
1. reference:参考某一资源ID。

(1)属性定义:         <declare-styleable name = "名称">                <attr name = "background" format = "reference" />         </declare-styleable> (2)属性使用:          <ImageView                  android:layout_width = "42dip"                  android:layout_height = "42dip"                  android:background = "@drawable/图片ID"                  /> 
  1. color:颜色值。

    (1)属性定义:

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

    (2)属性使用:

        <TextView              android:layout_width = "42dip"              android:layout_height = "42dip"              android:textColor = "#00FF00"              /> 
  2. boolean:布尔值。

    (1)属性定义:

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

    (2)属性使用:

        <Button             android:layout_width = "42dip"             android:layout_height = "42dip"             android:focusable = "true"             /> 
  3. dimension:尺寸值。

    (1)属性定义:

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

    (2)属性使用:

        <Button             android:layout_width = "42dip"             android:layout_height = "42dip"             /> 
  4. float:浮点值。

    (1)属性定义:

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

    (2)属性使用:

        <alpha            android:fromAlpha = "1.0"            android:toAlpha = "0.7"            /> 
  5. integer:整型值。

    (1)属性定义:

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

    (2)属性使用:

        <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"            /> 
  6. string:字符串。

    (1)属性定义:

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

    (2)属性使用:

        <com.google.android.maps.MapView             android:layout_width = "fill_parent"             android:layout_height = "fill_parent"             android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"             /> 
  7. fraction:百分数。

    (1)属性定义:

        <declare-styleable name="RotateDrawable">            <attr name = "visible" />            <attr name = "fromDegrees" format = "float" />            <attr name = "toDegrees" format = "float" />            <attr name = "pivotX" format = "fraction" />            <attr name = "pivotY" format = "fraction" />            <attr name = "drawable" />     </declare-styleable> 

    (2)属性使用:

        <rotate            xmlns:android = "http://schemas.android.com/apk/res/android" 

       android:interpolator = “@anim/动画ID”

               android:fromDegrees = "0" 

       android:toDegrees = “360”

               android:pivotX = "200%"            android:pivotY = "300%" 

       android:duration = “5000”

               android:repeatMode = "restart"            android:repeatCount = "infinite"            /> 
  8. enum:枚举值。

    (1)属性定义:

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

    (2)属性使用:

        <LinearLayout             xmlns:android = "http://schemas.android.com/apk/res/android"             android:orientation = "vertical"             android:layout_width = "fill_parent"             android:layout_height = "fill_parent"             >     </LinearLayout> 
  9. flag:位或运算。

    (1)属性定义:

         <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> 

    (2)属性使用:

        <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> 

    注意:

    属性定义时可以指定多种类型值。

0 0
原创粉丝点击