安卓自定义控件

ops/2024/10/18 7:46:39/

文章目录

      • 引入布局
      • 创建自定义控件

引入布局

  首先创建一个项目,创建一个空的活动。然后右键单击res/layout创建一个Layout Resource File文件,取名title.xml。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="wrap_content"><Buttonandroid:id="@+id/title_back"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_margin="5dp"android:text="Back"android:textColor="#fff" /><TextViewandroid: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="Tittle Text"android:textColor="#000"android:textSize="24sp" /><Buttonandroid:id="@+id/title_edit"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_margin="5dp"android:text="Edit"android:textColor="#fff" />
</LinearLayout>

  写好自定义布局之后进行引入。

<?xml version="1.0" encoding="utf-8"?>
<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>

  引入布局文件之后,修改活动文件。

java">public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 新加代码ActionBar actionbar = getSupportActionBar();if(actionbar != null){actionbar.hide();}// ---------------}
}

  调用getSupportActionBar()方法来获得ActionBar的实例,然后再调用它的hide()方法将标题栏隐藏起来。

在这里插入图片描述

创建自定义控件

  先创建一个TitleLayout文件,把它与title.xml文件关联起来。

java">public class TitleLayout extends LinearLayout {public TitleLayout(Context context, AttributeSet attrs){super(context, attrs);LayoutInflater.from(context).inflate(R.layout.title, this);}
}

  之后在activity_main.xml文件中添加这个自定义控件。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><!--修改内容--><com.example.uicustomviews.TitleLayoutandroid:layout_width="match_parent"android:layout_height="match_parent" /><!--修改内容-->
</LinearLayout>

  添加自定义控件和添加普通控件的方式基本是一样的,只不过在添加自定义控件的时候,我们需要指明控件的完整类名,包名在这里是不可以省略的。

  给标题栏添加点击事件。

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() {@Overridepublic void onClick(View v) {((Activity) getContext()).finish();}});titleEdit.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(getContext(), "You clicked Edit button",Toast.LENGTH_SHORT).show();}});// --------}
}

在这里插入图片描述


http://www.ppmy.cn/ops/89577.html

相关文章

经典图论算法回顾之Bellman-Ford算法

Dijkstra最短路径算法存在的一个问题是不能处理负权图&#xff08;详见&#xff1a;经典图论算法回顾之Dijkstra算法。今天要回顾的Bellman-Ford算法&#xff08;wikipedia&#xff1a;Bellman–Ford algorithm&#xff09;可以求出有负权图的最短路径&#xff0c;并可以对最短…

【前端 20】Element-UI快速入门

探索Element UI组件库&#xff1a;快速搭建Vue应用的必备工具 在现代Web开发中&#xff0c;Vue.js以其轻量级和灵活性赢得了广泛的关注。而Element UI&#xff0c;作为Vue.js的一个UI组件库&#xff0c;更是为开发者们提供了丰富、易用的前端组件&#xff0c;极大地加速了开发过…

TCL 实业 x TiDB丨从分销转向零售,如何考虑中台建设和数据库选型?

导读 在数字化转型的浪潮中&#xff0c;TCL 实业通过“新方舟”项目构建统一中台&#xff0c;实现了从分销向零售的转型&#xff0c;显著提升了业务精准度和效率。本文根据 InfoQ 记者高玉娴对 TCL 实业企业架构部架构师蔡玖发的采访整理&#xff0c;揭秘了 TCL 实业在这一转型…

Cocos Creator 3.8.x bundle核心知识点

bundle官网知识文档&#xff1a; https://docs.cocos.com/creator/3.8/manual/zh/asset/bundle.html bundle核心知识点如下&#xff1a;

【MATLAB源码】机器视觉与图像识别技术(7)续---BP神经网络

系列文章目录在最后面&#xff0c;各位同仁感兴趣可以看看&#xff01; BP神经网络 第一节、BP网络定义第二节、BP网络结构及其特点第三节、信息传播方式 信息的正向传播&#xff1a;实质是计算网络的输出误差的反向传播&#xff1a;实质是学习过程第四节、 BP网络的算法流程…

牛客JS题(十九)继承

注释很详细&#xff0c;直接上代码 涉及知识点&#xff1a; 构造函数实现类ES6类的写法原型链的应用 题干&#xff1a; 我的答案 <!DOCTYPE html> <html><head><meta charset"utf-8" /></head><body><script type"text…

SAP与九恒星资金系统集成案例(医药行业)

一、项目环境 江西某药业有限公司是一家以医药产业为主营、资本经营为平台的大型民营企业集团。公司成立迄今&#xff0c;企业经营一直呈现稳健、快速发展的态势集团总销售额超40亿元。 为了帮助企业更好的进行资金流、结算、资金调度和运作管理、风险控制&#xff0c;济民…

Vue+SpringBoot+SpringSecurity项目对于跨域的深度理解

随记&#xff08;可跳过&#xff09;&#xff1a;CodeMan在熬夜肝一周SpringSecurity学习的时候&#xff0c;总是报错&#xff0c;于是冥思苦想&#xff0c;选择了询问Ai&#xff0c;但是不论怎么设置权限&#xff0c;接口仍然无法按所设想的权限被调用&#xff0c;于是在今天的…