Android自定义控件的创建方法

Android自定义控件的创建方法,第1张

概述本文为大家分享了Android创建自定义控件的具体代码,供大家参考,具体内容如下

本文为大家分享了AndroID创建自定义控件的具体代码,供大家参考,具体内容如下

1、仿iPhone 的风格,在界面的顶部放置一个标题栏。

<?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="match_parent" > <linearLayout  androID:layout_wIDth="match_parent"  androID:layout_height="50dp"  androID:background="#2197db"  androID:orIEntation="horizontal"  androID:layout_alignParenttop="true"  androID:layout_alignParentleft="true"  androID:layout_alignParentStart="true">  <button   androID:ID="@+ID/Title_back"   androID:layout_wIDth="90dp"   androID:layout_height="40dp"   androID:layout_gravity="center"   androID:layout_margin="5dp"   androID:text="返回"   />  <TextVIEw   androID:ID="@+ID/Title_text"   androID:layout_wIDth="0dp"   androID:layout_height="wrap_content"   androID:layout_gravity="center"   androID:layout_weight="1"   androID:gravity="center"   androID:text="标题"   androID:textcolor="#fff"   androID:textSize="24sp"   />  <button   androID:ID="@+ID/Title_edit"   androID:layout_wIDth="90dp"   androID:layout_height="40dp"   androID:layout_gravity="center"   androID:layout_margin="5dp"   androID:text="确定"   /> </linearLayout></relativeLayout>

标题栏布局已经编写完成,剩下的就是如何在程序中使用这个标题栏。

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:layout_wIDth="match_parent"androID:layout_height="match_parent" ><include layout="@layout/Title" /></linearLayout>//我们只需要通过一行 include语句将标题栏布局引入进来就可以了。

然后在 MainActivity 中将系统自带的标题栏隐藏掉

public class MainActivity extends Activity {@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestwindowFeature(Window.FEATURE_NO_Title);setContentVIEw(R.layout.activity_main);}}

我们还是需要在每个活动中为这些控件单独编写一次事件注册的代码。比如说标题栏中的返回按钮,其实不管是在哪一个活动中,这个按钮的功能都是相同的,即销毁掉当前活动,这种情况最好是使用自定义控件的方式来解决。

新建自定义的标题栏控件:

public class TitleLayout extends linearLayout {public TitleLayout(Context context,AttributeSet attrs) {super(context,attrs);LayoutInflater.from(context).inflate(R.layout.Title,this);}}

我们重写了 linearLayout 中的带有两个参数的构造函数,在布局中引入 TitleLayout控件就会调用这个构造函数。然后在构造函数中需要对标题栏布局进行动态加载,这就要借助 LayoutInflater 来实现了。通过 LayoutInflater 的 from()方法可以构建出一个 LayoutInflater对象,然后调用 inflate()方法就可以动态加载一个布局文件,inflate()方法接收两个参数,第一个参数是要加载的布局文件的 ID,这里我们传入 R.layout.Title,第二个参数是给加载好的布局再添加一个父布局,这里我们想要指定为 TitleLayout,于是直接传入this

在布局文件中添加这个自定义控件 

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:layout_wIDth="match_parent"androID:layout_height="match_parent" ><com.example.xxxxxx.TitleLayoutandroID:layout_wIDth="match_parent"androID:layout_height="wrap_content"></com.example.xxxxxx.TitleLayout></linearLayout>

我们来尝试为标题栏中的按钮注册点击事件,修改 TitleLayout中的代码

public class TitleLayout extends linearLayout { public TitleLayout(Context context,AttributeSet attrs) {  super(context,attrs);  LayoutInflater.from(context).inflate(R.layout.Title,this);  button TitleBack = (button) findVIEwByID(R.ID.Title_back);  button TitleEdit = (button) findVIEwByID(R.ID.Title_edit);  TitleBack.setonClickListener(new OnClickListener() {   @OverrIDe   public voID onClick(VIEw v) {    ((Activity) getContext()).finish();   }  });  TitleEdit.setonClickListener(new OnClickListener() {   public static final String TAG = "";   @OverrIDe   public voID onClick(VIEw v) {    Toast.makeText(getContext(),"重新运行程序",Toast.LENGTH_SHORT).show();    Log.i(TAG,"111 ");   }  }); }}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。@H_403_48@

总结

以上是内存溢出为你收集整理的Android自定义控件的创建方法全部内容,希望文章能够帮你解决Android自定义控件的创建方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1146420.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存