Android自定义控件使用declare-styleable进行属性配置

Android自定义控件使用declare-styleable进行属性配置1、在res/vlaues文件夹下创建资源文件attrs.xml<?xmlversion=”1.0″encoding=”utf-8″?><resources><declare-styleablename=”poster”><attrname=”icon”format=”reference”/>…

Android自定义控件使用declare-styleable进行属性配置"

1、在res/vlaues文件夹下创建资源文件attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="poster">
        <attr name="icon" format="reference" />
        <attr name="iconW" format="dimension" />
        <attr name="iconH" format="dimension" />
        <attr name="text" format="reference|string" />
        <attr name="textColor" format="color" />
        <attr name="textSize" format="dimension" />
        <attr name="background" format="reference" />
    </declare-styleable>
    <declare-styleable name="settingItem">
        <attr name="name" format="reference|string" />
        <attr name="value" format="reference|string" />
    </declare-styleable>

</resources>

a. 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"

/>

b. color:颜色值

<declare-styleable name = "名称">

<attr name = "textColor" format = "color" />

</declare-styleable>

c. boolean:布尔值

<declare-styleable name = "名称">

<attr name = "focusable" format = "boolean" />

</declare-styleable>

d. dimension:尺寸值(注意:这里如果是dp那就会做像素转换)

<declare-styleable name = "名称">

<attr name = "layout_width" format = "dimension" />

</declare-styleable>

e. float:浮点值

f. integer:整型值

g. string:字符串

h. fraction:百分数

i. enum:枚举值

j. flag:是自己定义的,类似于 android:gravity=”top”,就是里面对应了自己的属性值

k. reference|color:颜色的资源文件

l.reference|boolean:布尔值的资源文件

注意://由于reference是从资源文件中获取,所以在XML文件中写这个属性的时候必须使用personattr:name=”@string/app_name”这种格式,否则会出错。

2.设置好属性文件后,在使用的布局中写相关配置

xmlns:myapp="http://schemas.android.com/apk/res/com.starview.tv"
<com.starview.tv.view.PosterView
            android:id="@+id/myview1"
            android:layout_width="290dp"
            android:layout_height="@dimen/settingpage_big_height"
            android:background="@drawable/hello_selection"
            myapp:background="@drawable/mainmenu_colorboard_blue"
            myapp:icon="@drawable/icons_01catbox_dtv"
            myapp:iconH="110dp"
            myapp:iconW="110dp"
            myapp:text="@string/str_dtv"
            myapp:textColor="@color/white"
            myapp:textSize="@dimen/indexsecondpagefontsize" />

3.最后在自定义控件的构造方法中获取你配置的属性值

package com.starview.tv.view;

import com.starview.tv.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

public class PosterView extends FrameLayout
{
     private ImageView mPosterIconImageV;
     private TextView mPosterTextV;
     private View mPosterBackground;

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

     public PosterView(Context context, AttributeSet attrs)
     {
          this(context, attrs, 0);
     }

     public PosterView(Context context, AttributeSet attrs, int defStyle)
     {
          super(context, attrs, defStyle);
          initView();
          TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.poster);
          if (mPosterTextV != null)
          {
               String text = a.getString(R.styleable.poster_text);
               if (text != null)
                    mPosterTextV.setText(text);
               int color = a.getColor(R.styleable.poster_textColor, Color.WHITE);
               mPosterTextV.setTextColor(color);
               float size = a.getDimensionPixelOffset(R.styleable.poster_textSize, 24);
               mPosterTextV.setTextSize(size);
          }
         
          if (mPosterIconImageV != null)
          {
               Drawable d = a.getDrawable(R.styleable.poster_icon);
               if(d != null)
                    mPosterIconImageV.setImageDrawable(d);
               int width = a.getDimensionPixelOffset(R.styleable.poster_iconW, 100);
               int height = a.getDimensionPixelOffset(R.styleable.poster_iconH, 100);
               ViewGroup.LayoutParams lp = mPosterIconImageV.getLayoutParams();
               lp.width = width;
               lp.height = height;
               mPosterIconImageV.setLayoutParams(lp);
          }
         
          if(mPosterBackground != null)
          {
               Drawable d = a.getDrawable(R.styleable.poster_background);
               if(d != null)
                    mPosterBackground.setBackgroundDrawable(d);
          }
          a.recycle();
     }

     private void initView()
     {
          LayoutInflater.from(getContext()).inflate(R.layout.poster_view_layout, this, true);
          mPosterBackground = findViewById(R.id.poster);
          mPosterIconImageV = (ImageView) findViewById(R.id.imageView1);
          mPosterTextV = (TextView) findViewById(R.id.textView1);
     }
    
     public void setImageDrawable(Drawable d)
     {
          if(mPosterIconImageV != null)
          {
               mPosterIconImageV.setImageDrawable(d);
          }
     }
    
     public void setImageDrawable(int resId)
     {
          if(mPosterIconImageV != null)
          {
               mPosterIconImageV.setImageResource(resId);
          }
     }
    
     public void setText(CharSequence text)
     {
          if(mPosterTextV != null)
          {
               mPosterTextV.setText(text);
          }
     }
    
     public void setText(int resId)
     {
          if(mPosterTextV != null)
          {
               mPosterTextV.setText(resId);
          }
     }
    
     public ImageView getIconImageView()
     {
          return mPosterIconImageV;
     }
    
     public TextView getTextView()
     {
          return mPosterTextV;
     }
    
     public void setTextVisibility(int visibility)
     {
          if(mPosterTextV != null)
          {
               mPosterTextV.setVisibility(visibility);
          }
     }
    
     public void setIconVisibility(int visibility)
     {
          if(mPosterIconImageV != null)
          {
               mPosterIconImageV.setVisibility(visibility);
          }
     }
    
     public void setPosterBackground(Drawable d)
     {
          if(mPosterBackground != null)
          {
               mPosterBackground.setBackgroundDrawable(d);
          }
     }
    
     public void setPosterBackground(int resId)
     {
          if(mPosterBackground != null)
          {
               mPosterBackground.setBackgroundResource(resId);
          }
     }
}

今天的文章Android自定义控件使用declare-styleable进行属性配置分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/31763.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注