自定义View步骤
来源:互联网 发布:拉法叶舰案 知乎 编辑:程序博客网 时间:2024/05/01 09:01
1.创建java类
package com.jerry.greentree.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.jerry.greentree.R;
import org.w3c.dom.Text;
/**
* Created by jerry on 15/11/3.
*/
public class HotelSelectItemView extends LinearLayout
{
private ImageView mIvHotel = null;
private TextView mTvTitle = null;
private TextView mTvContent = null;
public HotelSelectItemView(Context context)
{
this(context,null);
}
public HotelSelectItemView(Context context, AttributeSet attrs)
{
super(context, attrs);
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.select_item_layout,this);
mIvHotel = (ImageView) findViewById(R.id.iv_item);
mTvTitle = (TextView) findViewById(R.id.tv_tip);
mTvContent = (TextView) findViewById(R.id.tv_content);
initAttribute(context, attrs);
}
/**
* 初始化属性
* @param context
* @param attrs
*/
private void initAttribute(Context context, AttributeSet attrs)
{
TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.HotelSelect);
// 获取图片
Drawable icon =
array.getDrawable(R.styleable.HotelSelect_hotel_icon);
mIvHotel.setImageDrawable(icon);
// 获取文本
String title =
array.getString(R.styleable.HotelSelect_hotel_title);
mTvTitle.setText(title);
array.recycle();
}
/**
* 设置条件
* @param content
*/
public void setContent(String content)
{
mTvContent.setText(content);
}
public String getContent()
{
return mTvContent.getText().toString();
}
}
2.写入资源
<resources>
<declare-styleable name="HotelSelect">
<attr name="hotel_icon" format="reference"/>
<attr name="hotel_title" format="string"/>
</declare-styleable>
</resources>
3.布局调用
<com.jerry.greentree.view.HotelSelectItemView
android:id="@+id/item_city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
hotel:hotel_icon="@drawable/img_city"
hotel:hotel_title="@string/city"/>
- Android 自定义View步骤
- Android 自定义View步骤
- 自定义view步骤详解
- 自定义View步骤
- 自定义View步骤
- 自定义View的步骤
- 自定义View步骤
- Android 自定义view步骤
- 自定义view 基本步骤
- 自定义步骤View--StepView
- 自定义View步骤总结
- Android 自定义View步骤
- android自定义View实现步骤
- 自定义View的基本步骤
- Android自定义view的步骤
- 重识下自定义view基本步骤
- Android自定义view代码步骤
- 自定义view图形的步骤
- MySQL存储引擎
- eclipse 连接到数据库
- 注册验证码校验思路
- 《剑指offer》——孩子们的游戏(圆圈中最后剩下的数)
- GPU(CUDA)学习日记(九)------ CUDA存储器模型
- 自定义View步骤
- Android:SystemClock 随记
- Unreal Engine 4添加自定义Settings到项目设置
- MyBatis 插入空值时,需要指定JdbcType
- 判断字符串里面的左右自负是否被+包围
- usrp的API接口
- #三分法判断单峰函数最值#附加例题LA 5009
- 初学Redis(1)——认识Redis
- Android 源码解析: 图片加载库Picasso 3 核心类