最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android Studio 創(chuàng)建自定義控件的方法

 更新時間:2020年06月12日 14:49:42   作者:null;  
這篇文章主要介紹了Android Studio 創(chuàng)建自定義控件的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

我們知道,當(dāng)系統(tǒng)控件并不能滿足我們的需求時,我們就需要來創(chuàng)建自定義控件,主要有兩種方法

(1)引入布局

下面來自定義一個控件,iPhone的標(biāo)題欄,創(chuàng)建一個標(biāo)題欄并不是什么難事,加入兩個button一個TextView就行了,可是在我們的應(yīng)用中,有很多頁面都是需要這樣的標(biāo)題欄,我們不可能每個活動都寫一遍布局,這個時候我們就可以用引用布局的方法,新建一個title.xml

<?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="#817D7D"
  >

  <Button
    android: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"/>

  <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:textColor="#c0c0c0"
    android:textSize="24sp"
    android:text="title text" />

  <Button
    android:id="@+id/title_edit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="5dp"
    android:textColor="#fff"
    android:text="edit" />
</LinearLayout>

現(xiàn)在標(biāo)題欄已經(jīng)寫好了,接下來就要在程序中使用,修改activity_main.xml

<?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>

我們只要通過一句include語句引進(jìn)來就行了

 <include layout="@layout/title"/>

最后我們需要在MainActivity中將系統(tǒng)自帶的標(biāo)題欄屏蔽

package com.example.ch03;

import android.drm.DrmStore;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //屏蔽系統(tǒng)自帶狀態(tài)欄
    ActionBar actionBar = getSupportActionBar();
    if(actionBar != null){
      actionBar.hide();
    }
  }
}

最后來看一下效果

(2)注冊點(diǎn)擊事件

在上面我們看到,每個界面的返回按鈕功能都是一樣的,即銷毀當(dāng)前活動,我們不可能在每個活動中都重新注冊,所以使用自定義控件的方式來解決
新建TitleLayout,成為標(biāo)題欄控件

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

我們重寫了LinearLayout中帶參數(shù)的構(gòu)造函數(shù),引入TitleLayout控件就會調(diào)用這個構(gòu)造函數(shù),然后對標(biāo)題欄進(jìn)行動態(tài)加載,就需要借助LayoutInflater實(shí)現(xiàn)。通過LayoutInflater的from方法構(gòu)建一個LayoutInflater對象,調(diào)用inflate()方法動態(tài)加載一個布局文件

然后在布局文件中添加自定義控件,修改activity_main.xml

<?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"
 >
<com.example.ch03.TitleLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"/>

</LinearLayout>

重新運(yùn)行一下,效果是一樣的

下面來給按鈕注冊點(diǎn)擊事件,修改TitleLayout中的代碼

package com.example.ch03;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class TitleLayout extends LinearLayout {
public TitleLayout(Context context, AttributeSet attrs){
  super(context,attrs);
  LayoutInflater.from(context).inflate(R.layout.title,this);

  Button titleBack = findViewById(R.id.title_back);
  Button titleEdit = findViewById(R.id.title_edit);
  titleBack.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {

      ((Activity) getContext()).finish();
    }
  });
  titleEdit.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
      Toast.makeText(getContext(),"You click edit button",
          Toast.LENGTH_LONG).show();
    }
  });
}

}

重新運(yùn)行一下,然后點(diǎn)擊edit按鈕

到此這篇關(guān)于Android Studio 創(chuàng)建自定義控件的方法的文章就介紹到這了,更多相關(guān)Android Studio自定義控件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android實(shí)現(xiàn)長圖文截圖功能實(shí)例代碼

    Android實(shí)現(xiàn)長圖文截圖功能實(shí)例代碼

    這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)長圖文截圖功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • viewpager+photoview實(shí)現(xiàn)圖片查看器

    viewpager+photoview實(shí)現(xiàn)圖片查看器

    這篇文章主要為大家詳細(xì)介紹了viewpager+photoview實(shí)現(xiàn)圖片查看器,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • 詳解Android的四大應(yīng)用程序組件

    詳解Android的四大應(yīng)用程序組件

    這篇文章主要介紹了Android的應(yīng)用程序組件的相關(guān)資料,幫助大家更好的理解和使用Android,感興趣的朋友可以了解下
    2021-01-01
  • Kotlin?協(xié)程思維模型的引入使用建立

    Kotlin?協(xié)程思維模型的引入使用建立

    這篇文章主要為大家介紹了Kotlin?協(xié)程思維模型的引入使用及建立詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • 實(shí)例分析Android中HandlerThread線程用法

    實(shí)例分析Android中HandlerThread線程用法

    本篇文章主要給大家介紹了Android HandlerThread使用介紹以及源碼解析,有需要的朋友參考學(xué)習(xí)下吧。
    2017-12-12
  • Android開發(fā)實(shí)現(xiàn)瀏覽器全屏顯示功能

    Android開發(fā)實(shí)現(xiàn)瀏覽器全屏顯示功能

    這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)瀏覽器全屏顯示功能,涉及Android布局修改及相關(guān)屬性動態(tài)設(shè)置操作技巧,需要的朋友可以參考下
    2017-09-09
  • Android 通過Messager與Service實(shí)現(xiàn)進(jìn)程間雙向通信案例詳解

    Android 通過Messager與Service實(shí)現(xiàn)進(jìn)程間雙向通信案例詳解

    這篇文章主要介紹了Android 通過Messager與Service實(shí)現(xiàn)進(jìn)程間雙向通信案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • Android開發(fā)之繪制平面上的多邊形功能分析

    Android開發(fā)之繪制平面上的多邊形功能分析

    這篇文章主要介紹了Android開發(fā)之繪制平面上的多邊形功能,結(jié)合實(shí)例形式分析了Android多邊形圖形繪制的原理、步驟、相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2017-09-09
  • 在Android中創(chuàng)建widge組件的步驟

    在Android中創(chuàng)建widge組件的步驟

    Android Widget 是一種輕量級的小部件,可以直接在主屏幕上顯示實(shí)時數(shù)據(jù),提供簡單交互,它們主要用于展示簡單信息或快捷功能,幫助用戶更快、更方便地與應(yīng)用交互,接下來通過本文給大家介紹創(chuàng)建 Android Widget 的步驟,感興趣的朋友跟隨小編一起看看吧
    2024-08-08
  • 談?wù)剬ndroid View事件分發(fā)機(jī)制的理解

    談?wù)剬ndroid View事件分發(fā)機(jī)制的理解

    本篇文章主要介紹了談?wù)剬ndroid View事件分發(fā)機(jī)制的理解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01

最新評論

洞口县| 连州市| 五家渠市| 张家港市| 保康县| 天长市| 沙河市| 汉川市| 汉沽区| 武强县| 铜陵市| 剑川县| 丰镇市| 邓州市| 雅江县| 阿荣旗| 布尔津县| 布尔津县| 德化县| 垫江县| 普定县| 遵义市| 得荣县| 贵溪市| 海晏县| 伊宁市| 平安县| 南昌市| 靖宇县| 太原市| 钟祥市| 赞皇县| 夹江县| 乌兰察布市| 青龙| 石林| 台山市| 四平市| 咸丰县| 乐至县| 桦南县|