惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
F
Fortinet All Blogs
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
S
Secure Thoughts
SecWiki News
SecWiki News
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
N
Netflix TechBlog - Medium
Recorded Future
Recorded Future
Hacker News - Newest:
Hacker News - Newest: "LLM"
Webroot Blog
Webroot Blog
Cloudbric
Cloudbric
博客园 - 司徒正美
The Cloudflare Blog
W
WeLiveSecurity
T
Tailwind CSS Blog
V2EX - 技术
V2EX - 技术
H
Heimdal Security Blog
Jina AI
Jina AI
MyScale Blog
MyScale Blog
S
SegmentFault 最新的问题
Apple Machine Learning Research
Apple Machine Learning Research
雷峰网
雷峰网
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Project Zero
Project Zero
C
CXSECURITY Database RSS Feed - CXSecurity.com
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 【当耐特】
Forbes - Security
Forbes - Security
Last Week in AI
Last Week in AI
G
GRAHAM CLULEY
C
Check Point Blog
P
Proofpoint News Feed
L
LINUX DO - 最新话题
博客园 - Franky
P
Proofpoint News Feed
T
Tor Project blog
S
Security @ Cisco Blogs
Hugging Face - Blog
Hugging Face - Blog
阮一峰的网络日志
阮一峰的网络日志
J
Java Code Geeks
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
宝玉的分享
宝玉的分享
C
Cyber Attacks, Cyber Crime and Cyber Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
O
OpenAI News
小众软件
小众软件
云风的 BLOG
云风的 BLOG
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

CrazyWong

香港銀行開戶推薦 (附邀請碼) CrazyWong AI LOGO 🎧 Anson Seabra - Keep Your Head Up Princess 是的,我又更換了域名 1.5 萬字 CSS 基礎拾遺(核心知識、常見需求) JavaScript - 日期 Date JavaScript - 數組 Array 域名更換 前端 JavaScript 自測清單 2 前端 JavaScript 自測清單 1 通過travis-ci或者GitHub Actions自動化部署GitHub Pages和Coding Pages 添加Windows Terminal到鼠標右鍵菜單 調研實現高性能動畫 在瀏覽器輸入 URL 回車之後發生了什麼(超詳細版) hexo-theme-butterfly 安裝文檔(一)快速開始 設置Windows電腦自動關機 Windows必裝軟件推薦 關於字符編碼,你所需要知道的(ASCII,Unicode,Utf-8,GB2312…) 好用的新浪圖床工具推薦 - Weibo-Picture-Store Java知識點複習(二) Java知識點複習(一) Visual Studio Code 插件推薦-VSC Netease Music 重裝系統後重新部署恢復 Hexo blog Windows上Java的環境變量配置 Adapter 9Patch 介紹 第一行代碼筆記-RecyclerView 為Blog添加版權説明 第一行代碼筆記-ListView 第一行代碼筆記-四大Layout Hexo和Next主題的相關設置(持續更新) 第一行代碼筆記-bulid.gradle 解析 第一行代碼筆記-Android Studio工程目錄結構介紹 第一行代碼筆記-工具日志log
第一行代碼筆記-創建自定義控件
MYW · 2018-07-05 · via CrazyWong

第一行代碼筆記-創建自定義控件

自定義控件 - 引入布局

常見控件和布局的繼承結構

所有的控件都是直接或者間接繼承於 View
所有的布局都是直接或者間接繼承於 ViewGroup
View 是 Android 中最基本的一種 UI 控件,它可以在屏幕上繪製一塊矩形區域,並能響應這塊區域的各種事件。
ViewGroup 是特殊的一種 View,是一個用於放置控件和布局的容器

以添加 iPhone 風格的標題欄為例:
當多個活動界面都要使用這個標題欄時,我們可以通過引入布局的方式,這樣可以避免每個活動界面都要寫一遍同樣的標題代碼,減少代碼重複。

創建布局文件

新建一個 title.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?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="wrap_content"
android:background="@drawable/title_bg">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="@drawable/back_bg"
android:text="Back"
android:textColor="#fff"
android:id="@+id/title_back"/>

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Title Text"
android:id="@+id/title_text"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:textColor="#fff"
android:textSize="24sp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/title_edit"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="@drawable/edit_bg"
android:text="Edit"
android:textColor="#fff"/>

</LinearLayout>

android:background 用於為控件或布局制定一個背景,可以使用顏色或者圖片
android:layout_margin 指定控件在上下左右方向上偏移的距離

引入布局

在創建了布局後,我們要引入布局。在需要引入界面的 Activity 界面布局中,添加<include layout="@layout/title"/>

1
2
3
4
5
6
7
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include layout="@layout/title"/>
</LinearLayout>

隱藏系統自帶的標題欄

通過調用 getSupportActionBar()方法來獲得 ActionBar 的實例,然後再調用 ActionBar 的 hide()方法將標題欄隱藏。

1
2
3
4
5
6
7
8
9
10
11
12
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null){
actionBar.hide();
}
}
}

創建自定義控件

引入布局的方式雖然可以減少很多重複的布局代碼,但是當布局中的控件要求能夠響應事件,我們還是需要在每個活動中去為這些控件單獨編寫事件響應代碼。以標題欄為例,這些控件在每一個布局中所需的功能都是一樣的,這時我們可以使用自定義控件的方式,避免每個活動都要去編寫同樣的代碼。

新建 TitleLayout

新建 TitleLayout 並繼承 LinearLayout。

1
2
3
4
5
6
7
public class TitleLayout extends LinearLayout {

public TitleLayout(Context context , AttributeSet attributeSet){
super(context,attributeSet);
LayoutInflater.from(context).inflate(R.layout.title,this);
}
}

重寫 LinearLayout 中帶有兩個參數的構造函數。
通過 LayoutInflater 來對標題欄進行動態加載。
LayoutInflater.from(context).inflate(R.layout.title,this);

  • from()方法構建出一個 LayoutInflater 對象
  • 調用 inflate()方法就可以動態加載一個布局文件。inflate 接受兩個參數
    • 一個是加載的布局文件 id
    • 另一個是給加載好的布局再添加一個父布局

修改活動布局.xml

1
2
3
4
5
6
7
8
9
10
11
12
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

//完整類名
<com.example.hwy01.uicustomviews.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"></com.example.hwy01.uicustomviews.TitleLayout>

</LinearLayout>

添加自定義控件需要先指明控件的完整類名,如代碼中的com.example.hwy01.uicustomviews.TitleLayout

添加按鈕註冊事件

修改 TitleLayout

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class TitleLayout extends LinearLayout {

public TitleLayout(Context context , AttributeSet attributeSet){
super(context,attributeSet);

LayoutInflater.from(context).inflate(R.layout.title,this);
Button back = (Button) findViewById(R.id.title_back);
Button edit = (Button) findViewById(R.id.title_edit);

back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
((Activity)getContext()).finish();
}
});

edit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getContext(),"You clicked Edit Button",Toast.LENGTH_SHORT).show();
}
});
}

}

版權聲明: 本部落格所有文章除特別聲明外,均採用CC BY-NC-SA 4.0 授權協議。轉載請註明來源 CrazyWong