自定义itemClickView

640?wx_fmt=gif

640?wx_fmt=gif

640?wx_fmt=other

极力推荐文章:欢迎收藏
Android 干货分享 

640?wx_fmt=other

本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以下内容:

  1. 自定义View类实现

  2. 自定义View标签

  3. 自定义View 布局

  4. 自定义View 选择器

  5. 自定义View 素材

  6. Activity使用自定义View

ItemClickViewAndroid非常常用,此实现效果类似于Android Settings界面,
实现效果如下:

640?wx_fmt=other

自定义itemClickView

1. 自定义View类实现

public class ItemClickView extends RelativeLayout {
private static final String TAG = "ItemClickView";
private TextView tv_title;
private TextView tv_des;

public ItemClickView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
        initUI(context);
    }

public ItemClickView(Context context, AttributeSet attrs) {
super(context, attrs);
        initUI(context);
    }

public ItemClickView(Context context) {
super(context);
        initUI(context);
    }

// 单独抽取出来的 xml--->view
private void initUI(Context context) {
        View.inflate(context, R.layout.item_click_view, this);

        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_des = (TextView) findViewById(R.id.tv_des);
    }

/**
     * @param title
     *            要修改成的标题内容 修改标题的方法
     */
public void setTitle(String title) {
        tv_title.setText(title);
    }

/**
     * @param des
     *            描述内容字符串 修改描述内容方法
     */
public void setDes(String des) {
        tv_des.setText(des);
    }
}
public class ItemClickView extends RelativeLayout {
private static final String TAG = "ItemClickView";
private TextView tv_title;
private TextView tv_des;

public ItemClickView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
        initUI(context);
    }

public ItemClickView(Context context, AttributeSet attrs) {
super(context, attrs);
        initUI(context);
    }

public ItemClickView(Context context) {
super(context);
        initUI(context);
    }

// 单独抽取出来的 xml--->view
private void initUI(Context context) {
        View.inflate(context, R.layout.item_click_view, this);

        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_des = (TextView) findViewById(R.id.tv_des);
    }

/**
     * @param title
     *            要修改成的标题内容 修改标题的方法
     */
public void setTitle(String title) {
        tv_title.setText(title);
    }

/**
     * @param des
     *            描述内容字符串 修改描述内容方法
     */
public void setDes(String des) {
        tv_des.setText(des);
    }
}

2. 自定义View标签

    <com.programandroid.CustomView.ItemClickView
        android:id="@+id/custom_item_click_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/listview_item_selector" />

3. 自定义View 布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
     >

<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/primary_text_light"
android:textSize="18sp" />

<TextView
android:id="@+id/tv_des"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_title"
android:textColor="@android:color/secondary_text_light"
android:textSize="14sp" />

<ImageView
android:id="@+id/iv_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="@drawable/arrow_right_selector" />


</RelativeLayout>

4. 自定义View 选择器

  • 箭头选择器arrow_right_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 按下状态 -->
<item android:drawable="@drawable/arrow_right_pressed" android:state_pressed="true"/>
<!-- 获取焦点 -->
<item android:drawable="@drawable/arrow_right_pressed" android:state_focused="true"/>
<!-- 正常状态 -->
<item android:drawable="@drawable/arrow_right_normal"/>
</selector>

  • item选择器 listview_item_selector .xml

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- 按下去的背景颜色显示效果 -->
<item android:drawable="@drawable/list_item_bg_light_pressed" android:state_pressed="true"/>
<!-- 获取焦点时背景颜色显示效果 -->
<item android:drawable="@drawable/list_item_bg_light_pressed" android:state_focused="true"/>
<!-- 没有任何状态下的背景颜色 -->
<item android:drawable="@drawable/list_item_bg_light_normal"/>

</selector>

5. 自定义View 素材

640?wx_fmt=other

list_item_bg_light_normal.9.png


640?wx_fmt=other

list_item_bg_light_pressed.9.png

640?wx_fmt=other

arrow_right_pressed.png


640?wx_fmt=other

arrow_right_normal.png

6. Activity使用自定义View

Activity 使用自定义View的方法如下:

    /**
     * 自定义 ItemClickView 调用
     */
private void InitItemClickView() {
// TODO Auto-generated method stub
        ItemClickView mItemClickView = (ItemClickView) findViewById(R.id.custom_item_click_view);
        mItemClickView.setTitle("About Phone");
        mItemClickView.setDes("Android 7.0");
        mItemClickView.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "点击自定义View,获取更多内容", 0)
                        .show();
            }
        });

    }
    /**
     * 自定义 ItemClickView 调用
     */
private void InitItemClickView() {
// TODO Auto-generated method stub
        ItemClickView mItemClickView = (ItemClickView) findViewById(R.id.custom_item_click_view);
        mItemClickView.setTitle("About Phone");
        mItemClickView.setDes("Android 7.0");
        mItemClickView.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "点击自定义View,获取更多内容", 0)
                        .show();
            }
        });

    }

640?wx_fmt=jpeg

640?wx_fmt=jpeg

长按识别二维码,领福利

至此,本篇已结束,如有不对的地方,欢迎您的建议与指正。同时期待您的关注,感谢您的阅读,谢谢!

如有侵权,请联系小编,小编对此深感抱歉,届时小编会删除文章,立即停止侵权行为,请您多多包涵。

640?wx_fmt=gif

640?wx_fmt=png


posted @   程序员Android的博客  阅读(79)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
点击右上角即可分享
微信分享提示