• Android自定义组件之简单组合


    Android自定义控件有两种,一种是组合。比如一个linearlayout 里面有textview,imageview.
    这样的好处是,写一个就可以多处使用。

    1. view_image_and_button.xml
      你的组合控件的布局文件
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_gravity="center"
        android:layout_centerInParent="true"
        android:layout_centerHorizontal="true"
    
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/img_top"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"
            />
        <TextView
            android:id="@+id/tv_bottom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
    
    
            android:text="aaa"
    
            />
    
    </LinearLayout>

    2.自定义属性

    appsrcmain esvaluesattrs.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <declare-styleable name="ImageBtnWithText">
        <attr name="text" format="string"/>
        <attr name="src" format="reference"/>
          </declare-styleable>
    
    </resources>

    注释:
    参考:http://www.jb51.net/article/32172.htm

    1. reference:参考某一资源ID。
      示例:
    <declare-styleable name = "名称"> 
    <attr name = "background" format = "reference" /> 
    <attr name = "src" format = "reference" /> 
    </declare-styleable> 
    1. color:颜色值。
    <declare-styleable name = "名称"> 
    <attr name = "textColor" format = "color" /> 
    </declare-styleable> 
    1. boolean:布尔值。
    示例: 
    [java] 
    复制代码 代码如下:
    
    <declare-styleable name = "名称"> 
    <attr name = "focusable" format = "boolean" /> 
    </declare-styleable> 
    1. dimension:尺寸值。
    示例: 
    [java] 
    复制代码 代码如下:
    
    <declare-styleable name = "名称"> 
    <attr name = "layout_width" format = "dimension" /> 
    </declare-styleable> 
    1. float:浮点值。
    示例: 
    [java] 
    复制代码 代码如下:
    
    <declare-styleable name = "名称"> 
    <attr name = "fromAlpha" format = "float" /> 
    <attr name = "toAlpha" format = "float" /> 
    </declare-styleable> 
    1. integer:整型值。
    示例: 
    [java] 
    复制代码 代码如下:
    
    <declare-styleable name = "名称"> 
    <attr name = "frameDuration" format="integer" /> 
    <attr name = "framesCount" format="integer" /> 
    </declare-styleable> 
    1. string:字符串。
    示例: 
    [java] 
    复制代码 代码如下:
    
    <declare-styleable name = "名称"> 
    <attr name = "text" format = "string" /> 
    </declare-styleable> 
    1. fraction:百分数。
    示例: 
    [java] 
    复制代码 代码如下:
    
    <declare-styleable name="名称"> 
    <attr name = "pivotX" format = "fraction" /> 
    <attr name = "pivotY" format = "fraction" /> 
    </declare-styleable> 
    1. enum:枚举值。
    示例: 
    [java] 
    复制代码 代码如下:
    
    <declare-styleable name="名称"> 
    <attr name="orientation"> 
    <enum name="horizontal" value="0" /> 
    <enum name="vertical" value="1" /> 
    </attr> 
    </declare-styleable> 
    1. flag:位或运算。
    示例: 
    [java] 
    复制代码 代码如下:
    
    <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" /> 
    </attr> 
    </declare-styleable> 

    11.多类型。

    示例: 
    [java] 
    复制代码 代码如下:
    
    <declare-styleable name = "名称"> 
    <attr name = "background" format = "reference|color" /> 
    </declare-styleable> 

    3.代码

    
    
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.util.AttributeSet;
    import android.view.LayoutInflater;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    
    import com.chinaCEB.cebActivity.R;
    
    /**
     * Created by Administrator on 2016/6/22.
     */
    public class ImageAndButton extends LinearLayout {
    
        private ImageView imageView;
        private TextView textView;
    
        public ImageAndButton(Context context) {
            super(context);
        }
    
        public ImageAndButton(Context context, AttributeSet attrs) {
            super(context, attrs);
            LinearLayout linearLayout= (LinearLayout) LayoutInflater.from(context).inflate(R.layout.view_image_and_button,this,true);
            imageView = (ImageView) linearLayout.findViewById(R.id.img_top);
            textView = (TextView) linearLayout.findViewById(R.id.tv_bottom);
            TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.ImageBtnWithText);
            textView.setText(typedArray.getText(R.styleable.ImageBtnWithText_text));
            imageView.setImageResource(typedArray.getResourceId(R.styleable.ImageBtnWithText_src,R.mipmap.exit));
        }
    
        public ImageAndButton(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        public void setImageView(int resourseId){
            imageView.setImageResource(resourseId);
        }
    
        public void setTextView(String string){
            textView.setText(string);
        }
    
    }
    

    4.使用:
    注意:要加上下面这句:
    xmlns:xinyu=”http://schemas.android.com/apk/res-auto”

    <com.chinaCEB.cebActivity.widget.ImageAndButton
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:layout_height="match_parent"
                    xinyu:src="@mipmap/menu_item_icon_shake_selected"
                    xinyu:text="@string/shuifei"
                    >
    
                </com.chinaCEB.cebActivity.widget.ImageAndButton>
  • 相关阅读:
    iOS工作笔记(十二)
    隐藏底部导航栏tabbar
    SDWebImage的使用及图片不改变问题的解决
    NSString转化为NSDictionary
    makeObjectsPerformSelector用法
    iOS中点与像素
    iOS动画的简单介绍
    Pods安装时卡在Updating local specs repositories界面时的处理
    使用uicollectionView时需要注意的问题
    Sublime Text 3 自定义配置快捷键
  • 原文地址:https://www.cnblogs.com/caoxinyu/p/6647846.html
Copyright © 2020-2023  润新知