自定义控件的基本要求

这篇文章就当是自定义控件入门,看了几篇android关于自定义控件的文章,了解了一下,android自定义控件主要有3种方式:

  1. 自绘控件:继承View类,所展示的内容在OnDraw方法中绘制出来
  2. 组合控件:不需要绘制视图显示的内容,只用系统原生的控件,将几个控件组合起来,(这就是这篇文章要写的自定义标题栏)
  3. 继承控件:继承原生的控件类,在原生的属性上增加新的功能。

这篇文章所要写的是第二种方式组合控件,来实现自定义标题栏。总结这4点实现一个组合控件的基本要求:

1.在XML布局中可设置组合控件自定义的属性。

2.在代码总可设置属性和方法。

3.UI交互:布局美观,按下,点击等效果。

4.自定义回调事件

先来看看最终实现的效果图:

自定义标题栏的实现(使用的是include标签)

自定义标题栏的好处:

  • 提高布局效率
  • 降低布局文件的维护成本
  • 方便使用,容易扩展
  • 降低标题栏和Activity代码逻辑的耦合

我们先来看看布局文件:TitleBar.axml

<?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="56dp">
<Button
android:id="@+id/titleBar_left_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:text="返回"
android:drawableLeft="@drawable/icon_white_arrow_left"
android:background="@android:color/transparent"
android:textSize="14sp" />
<TextView
android:id="@+id/title_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="测试标题"
android:singleLine="true"
android:textSize="17sp" />
<Button
android:id="@+id/titleBar_right_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:text="提交"
android:textSize="14sp"
android:background="@android:color/transparent"/>
</RelativeLayout>

然后在需要的地方通过include标签引用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/TitleBar" />
</LinearLayout>

在MainActivity中添加单击事件,或者设置属性

    [Activity(Label = "CustomeTitleBar", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
private TextView title_bar_title;
private Button title_bar_left_btn;
private Button title_bar_right_btn;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView (Resource.Layout.Main);
title_bar_left_btn = FindViewById<Button>(Resource.Id.titleBar_left_btn);
title_bar_right_btn = FindViewById<Button>(Resource.Id.titleBar_right_btn);
title_bar_title = FindViewById<TextView>(Resource.Id.title_bar_title);
title_bar_title.Text = "新的标题";
title_bar_left_btn.Click += (s, e) => {
Finish();
};
title_bar_right_btn.Click += (s, e) =>
{
Toast.MakeText(this,"提交",ToastLength.Short).Show();
};
}
}

上面的这种方式与我们自定义控件的要求相差较远,不能自定义属性,不能事件的回调。并不推荐这种方式。如果说一直用include标签的话,这个自定义标题栏好像是写给自己用的似的。

自定义标题栏的实现(自定义属性、回调事件)

(1)定义标题栏的组合布局

我们还是写来自定义一个布局,还是用上面的那个布局,不过最外层的根布局RelativeLayout就不要了,使用merge标签,避免这种嵌套布局

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/titleBar_left_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:gravity="left|center_vertical"
android:background="@android:color/transparent"
android:textSize="14sp" />
<TextView
android:id="@+id/title_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:singleLine="true"
android:textSize="17sp" />
<Button
android:id="@+id/titleBar_right_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:background="@android:color/transparent"
android:gravity="right|center_vertical"
android:textSize="14sp" />
</merge>

(2)自定标题栏相关的自定义属性

在values文件夹新建一个xml文件attrs.xml,关于android中自定义属性format的取值类型有以下这些:

  1. reference:如android:background = “@drawable/图片ID”
  2. color:如android:textColor = “#000000”
  3. boolean:如android:focusable = “false”
  4. dimenion:如android:layout_width = “10dp”
  5. float :如android:fromAlpha = “1.0”
  6. integer:如android:background = “@drawable/图片ID”
  7. string:如android:text=”123”
  8. fraction:百分数如:android:pivotY = “300%”
  9. enum:如android:orientation=”vertical”
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<declare-styleable name="CustomeTitleBar">
<attr name="title_background_color" format="color"/>
<attr name="title_text" format="string"/>
<attr name="title_text_color" format="color"/> <attr name="right_button_text" format="string"/>
<attr name="right_button_text_color" format="color"/>
<attr name="right_button_drawable" format="reference|integer"/>
<attr name="right_button_visible" format="boolean"/> <attr name="left_button_text" format="string"/>
<attr name="left_button_text_color" format="color"/>
<attr name="left_button_drawable" format="reference|integer"/>
<attr name="left_button_visible" format="boolean"/>
</declare-styleable>
</resources>

(3)自定义标题栏代码的实现,根据不同的需求继承不同的原生ViewGroup类,这里继承的是RelativeLayout,也可以LinearLayout、EditText。

    public class MyTitleBar : RelativeLayout
{
private TextView title_bar_title;
private Button title_bar_left_btn;
private Button title_bar_right_btn;
public MyTitleBar(Context context, IAttributeSet attrs) : base(context, attrs)
{
//base(context, attrs);
LayoutInflater.From(context).Inflate(Resource.Layout.TitleBar, this, true);
title_bar_left_btn = FindViewById<Button>(Resource.Id.titleBar_left_btn);
title_bar_right_btn = FindViewById<Button>(Resource.Id.titleBar_right_btn);
title_bar_title = FindViewById<TextView>(Resource.Id.title_bar_title); try
{
TypedArray attributes = context.ObtainStyledAttributes(attrs, Resource.Styleable.CustomeTitleBar);
if (attributes != null)
{
//titlebar 背景颜色 int titleBarBackground = attributes.GetResourceId(Resource.Styleable.CustomeTitleBar_title_background_color,Resource.Color.color_primary);
SetBackgroundResource(titleBarBackground); //左边按钮
//是否显示
bool leftButtonVisible = attributes.GetBoolean(Resource.Styleable.CustomeTitleBar_left_button_visible, true);
if (leftButtonVisible)
{
title_bar_left_btn.Visibility = ViewStates.Visible;
}
else
{
title_bar_left_btn.Visibility = ViewStates.Gone;
} //设置左边按钮的文字和图标(二者只能选其一)
string leftButtonText = attributes.GetString(Resource.Styleable.CustomeTitleBar_left_button_text);
if (!string.IsNullOrEmpty(leftButtonText))
{
title_bar_left_btn.Text = leftButtonText;
//设置左边按钮的文字颜色
Color leftButtonTextColor = attributes.GetColor(Resource.Styleable.CustomeTitleBar_left_button_text_color, Color.White);
title_bar_left_btn.SetTextColor(leftButtonTextColor);
}
else //(不设置文本,就只能设置图标)
{
int leftButtonDrawable = attributes.GetResourceId(Resource.Styleable.CustomeTitleBar_left_button_drawable, Resource.Drawable.icon_white_arrow_left);
if (leftButtonDrawable != -1)
{
Drawable drawable = Resources.GetDrawable(leftButtonDrawable);
drawable.SetBounds(0, 0, drawable.MinimumWidth,drawable.MinimumHeight);//不设置这句图标显示不出来
title_bar_left_btn.SetCompoundDrawables(drawable,null,null,null);
}
} //右边按钮
//是否显示
bool rightButtonVisible = attributes.GetBoolean(Resource.Styleable.CustomeTitleBar_right_button_visible, true);
if (rightButtonVisible)
{
title_bar_right_btn.Visibility = ViewStates.Visible;
}
else
{
title_bar_right_btn.Visibility = ViewStates.Gone;
} //设置左边按钮的文字和图标(二者只能选其一)
string rightButtonText = attributes.GetString(Resource.Styleable.CustomeTitleBar_right_button_text);
if (!string.IsNullOrEmpty(rightButtonText))
{
title_bar_right_btn.Text = rightButtonText;
//设置左边按钮的文字颜色
Color leftButtonTextColor = attributes.GetColor(Resource.Styleable.CustomeTitleBar_right_button_text_color, Color.White);
title_bar_right_btn.SetTextColor(leftButtonTextColor);
}
else //(不设置文本,就只能设置图标)
{
int rightButtonDrawable = attributes.GetResourceId(Resource.Styleable.CustomeTitleBar_right_button_drawable, Resource.Drawable.icon_white_arrow_left);
if (rightButtonDrawable != -1)
{
Drawable drawable = Resources.GetDrawable(rightButtonDrawable);
drawable.SetBounds(0,0,drawable.MinimumHeight,drawable.MinimumHeight);
title_bar_right_btn.SetCompoundDrawables(null, null,drawable, null);
}
}
//处理标题
string titleText = attributes.GetString(Resource.Styleable.CustomeTitleBar_title_text);
if (!string.IsNullOrEmpty(titleText))
{
title_bar_title.Text = titleText;
}
Color color = attributes.GetColor(Resource.Styleable.CustomeTitleBar_title_text_color,Color.White);
title_bar_title.SetTextColor(color);
attributes.Recycle();
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.Write(ex.ToString());
}
} //设置事件的监听
public void SetTitleClickListener(IOnClickListener onClickListener)
{
if (onClickListener != null)
{
title_bar_left_btn.SetOnClickListener(onClickListener);
title_bar_right_btn.SetOnClickListener(onClickListener);
}
}
//获取左边的Button
public Button GetTitleBarLeftBtn()
{
return title_bar_left_btn;
}
//获取右边的Button
public Button GetTitleRightBtn()
{
return title_bar_right_btn;
}
//获取标题
public TextView GetTitleBarTitle()
{
return title_bar_title;
}
}

(4)使用自定义标题栏,并且自定义属性

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mview="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CustomeTitleBar.MyTitleBar
android:id="@+id/myTitleBar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
mview:title_text="测试标题"
mview:right_button_text=""
mview:right_button_text_color="@color/color_white"
mview:right_button_visible="true"
mview:left_button_text="返回"/>
<CustomeTitleBar.MyTitleBar
android:id="@+id/myTitleBar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
mview:title_text="标题2"
mview:right_button_text="提交"
mview:right_button_text_color="@color/color_white"
mview:left_button_text="返回"
mview:left_button_text_color="@color/color_white"/>
<CustomeTitleBar.MyTitleBar
android:id="@+id/myTitleBar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
mview:title_text="标题2"
mview:right_button_visible="false"
mview:left_button_text="返回"
mview:left_button_text_color="@color/color_white"/>
<CustomeTitleBar.MyTitleBar
android:id="@+id/myTitleBar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
mview:title_text="标题2"
mview:title_background_color="@color/color_red"
mview:right_button_text_color="@color/color_white"
mview:left_button_text="返回"
mview:left_button_text_color="@color/color_red"/>
</LinearLayout>

上面的代码基本实现了与activity逻辑代码的分离、UI界面要求、可配置自定义属性的要求,可最关键的事件的回调还没写,的确这是最核心的地方,这个自定义标题栏的两个按钮的单击事件最终还是要在activity中去调用。

这个就留着下次再写吧。

如果觉得还有点懵逼的话,看看github:https://github.com/MaChuZhang/Xamarin-Android-Custom-View/tree/master/CustomeTitleBar

作者:张林

标题:xamarin android自定义标题栏(自定义属性、回调事件)

原文地址:http://blog.csdn.net/kebi007/article/details/75365917

转载随意注明出处

[置顶] xamarin android自定义标题栏(自定义属性、回调事件)的更多相关文章

  1. [置顶] xamarin android自定义spinner

    以前弄的一个下拉框时自带的spinner,感觉好丑,实际效果实在满足不了基本的UI界面要求,还是自己动手丰衣足食,看了网上关于android中自定义spinner的文章,感觉实现原理还是比较简单,所以 ...

  2. [置顶] Xamarin android沉浸式状态栏

    虽然关于android "沉浸式"状态栏有很多博客介绍过,从小菜到大神无一例外.我第一次看到这种"沉浸"式的效果我也以为真的是这么叫,然而根本不是这么回事,完全 ...

  3. [置顶] xamarin android使用zxing扫描二维码

    好久没写了,这片文章篇幅不长,概述一下在xamarin android中用 ZXing.Net.Mobile库扫描二维码读取url的示例.扫码支付,扫码登录,App上各种各样的扫码,好像没个扫码的就有 ...

  4. [置顶] xamarin android toolbar(踩坑完全入门详解)

    网上关于toolbar的教程有很多,很多新手,在使用toolbar的时候踩坑实在太多了,不好好总结一下,实在浪费.如果你想学习toolbar,你肯定会去去搜索androd toolbar,既然你能看到 ...

  5. [置顶] xamarin android使用gps定位获取经纬度

    看了文章你会得出以下几个结论 1.android定位主要有四种方式GPS,Network(wifi定位.基站定位),AGPS定位 2.绝大部分android国产手机使用network进行定位是没有作用 ...

  6. [置顶] xamarin android 布局尺寸了解

    为了使UI界面在不同大小的移动端显示器上能够正常显示,大家可能都知道使用sp作为字体大小的单位,dp作为其他元素长度的单位. 前几天看了一篇文章关于 App设计规范的,文章用心写的非常好,这里是链接  ...

  7. [置顶] xamarin android Fragment实现底部导航栏

    前段时间写了篇关于Fragment的文章,介绍了基础的概念,用静态和动态的方式加载Fragment  Xamarin Android Fragment的两种加载方式.下面的这个例子介绍xamarin ...

  8. [置顶] Xamarin android中使用signalr实现即时通讯

    前面几天也写了一些signalr的例子,不过都是在Web端,今天我就来实践一下如何在xamarin android中使用signalr,刚好工作中也用到了这个,也算是总结一下学到的东西吧,希望能帮助你 ...

  9. [置顶] Xamarin android如何调用百度地图入门示例(一)

    在Xamarin android如何调用百度地图呢? 首先我们要区分清楚,百度地图这是一个广泛的概念,很多刚刚接触这个名词"百度地图api",的确是泛泛而谈,我们来看一下百度地图的 ...

随机推荐

  1. Java学习笔记12---向上转型-父类的对象引用指向子类对象

    当父类的对象引用没有指向父类的对象,而是指向了子类的对象时,调用方法或访问变量时会怎样呢? 假设父类为Person,子类为Student,有下面的两行定义: Student sTest = new S ...

  2. lua API 小记2

    1. 创建lua虚拟机 lua_State *lua_newstate (lua_Alloc f, void *ud) 创建一个新的独立的lua虚拟机. 参数指定了内存分配策略及其参数, 注意, 让用 ...

  3. 用python实现一个简单的词云

    对于在windows(Pycharm工具)里实现一个简单的词云还是经过了几步小挫折,跟大家分享下,如果遇到类似问题可以参考: 1. 导入wordcloud包时候报错,当然很明显没有安装此包. 2. 安 ...

  4. 《java.util.concurrent 包源码阅读》09 线程池系列之介绍篇

    concurrent包中Executor接口的主要类的关系图如下: Executor接口非常单一,就是执行一个Runnable的命令. public interface Executor { void ...

  5. 十二、VueJs 填坑日记之项目打包发布

    通过上一篇博文的学习,我们其实已经完成了我们设想的项目的开发.但是,我们做好的这套东西,是基于 nodejs 开发的.而我们最终希望,我们开发的项目,生成好一堆文件,然后随便通过任何一个 http 服 ...

  6. css随笔属性

    anchor伪类,用于阅读文章.a:link(没有接触过的链接),用于链接常规状态 (末访问的链接)a:hover(鼠标放在链接上的状态) 用于产生视觉效果(已访问的链接)a:visited(访问过的 ...

  7. ubuntu debain下好用的编辑器

    geany: 轻量级的IDE apt-get install geany 用来写shell脚本和python十分方便.特别写python脚本时,它有丰富的提示和自动补全功能.查看代码也很方便

  8. Spring MVC 学习总结(九)——Spring MVC实现RESTful与JSON(Spring MVC为前端提供服务)

    很多时候前端都需要调用后台服务实现交互功能,常见的数据交换格式多是JSON或XML,这里主要讲解Spring MVC为前端提供JSON格式的数据并实现与前台交互.RESTful则是一种软件架构风格.设 ...

  9. 40.Linux应用调试-使用gdb和gdbserver

    1.gdb和gdbserver调试原理 通过linux虚拟机里的gdb,来向开发板里的gdbserver发送命令,比如设置断点,运行setp等,然后开发板上的gdbserver收到命令后,便会执行应用 ...

  10. 深入常用CSS声明(一) —— Background

    一直对一些自己常用的css声明掌握得不是很全,只知道常用的一些属性和值,但是对于其他的用法确实一知半解,这篇文章旨在扫盲,先不说有多深的理解,至少做到能够看到这些声明的属性和值的时候做到不陌生. 这里 ...