Android自定义组件之简单组合
Android自定义控件有两种,一种是组合。比如一个linearlayout 里面有textview,imageview.
这样的好处是,写一个就可以多处使用。
- 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.自定义属性
app\src\main\res\values\attrs.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
- reference:参考某一资源ID。
示例:
<declare-styleable name = "名称">
<attr name = "background" format = "reference" />
<attr name = "src" format = "reference" />
</declare-styleable>
- color:颜色值。
<declare-styleable name = "名称">
<attr name = "textColor" format = "color" />
</declare-styleable>
- boolean:布尔值。
示例:
[java]
复制代码 代码如下:
<declare-styleable name = "名称">
<attr name = "focusable" format = "boolean" />
</declare-styleable>
- dimension:尺寸值。
示例:
[java]
复制代码 代码如下:
<declare-styleable name = "名称">
<attr name = "layout_width" format = "dimension" />
</declare-styleable>
- float:浮点值。
示例:
[java]
复制代码 代码如下:
<declare-styleable name = "名称">
<attr name = "fromAlpha" format = "float" />
<attr name = "toAlpha" format = "float" />
</declare-styleable>
- integer:整型值。
示例:
[java]
复制代码 代码如下:
<declare-styleable name = "名称">
<attr name = "frameDuration" format="integer" />
<attr name = "framesCount" format="integer" />
</declare-styleable>
- string:字符串。
示例:
[java]
复制代码 代码如下:
<declare-styleable name = "名称">
<attr name = "text" format = "string" />
</declare-styleable>
- fraction:百分数。
示例:
[java]
复制代码 代码如下:
<declare-styleable name="名称">
<attr name = "pivotX" format = "fraction" />
<attr name = "pivotY" format = "fraction" />
</declare-styleable>
- enum:枚举值。
示例:
[java]
复制代码 代码如下:
<declare-styleable name="名称">
<attr name="orientation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
</declare-styleable>
- 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>
泰山崩于前而色不变的曹新雨,抱歉