欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

(1)引入布局

下面來自定義一個控件,iPhone的標題欄,創(chuàng)建一個標題欄并不是什么難事,加入兩個button一個TextView就行了,可是在我們的應用中,有很多頁面都是需要這樣的標題欄,我們不可能每個活動都寫一遍布局,這個時候我們就可以用引用布局的方法,新建一個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)在標題欄已經(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語句引進來就行了

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

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

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)注冊點擊事件

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

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

我們重寫了LinearLayout中帶參數(shù)的構造函數(shù),引入TitleLayout控件就會調(diào)用這個構造函數(shù),然后對標題欄進行動態(tài)加載,就需要借助LayoutInflater實現(xiàn)。通過LayoutInflater的from方法構建一個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>

重新運行一下,效果是一樣的

下面來給按鈕注冊點擊事件,修改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();
    }
  });
}

}

重新運行一下,然后點擊edit按鈕

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

相關文章

  • Android拋物線下載動畫制作過程

    Android拋物線下載動畫制作過程

    這篇文章主要為大家詳細介紹了Android拋物線下載動畫制作過程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • 詳解Android開發(fā)中Activity的四種launchMode

    詳解Android開發(fā)中Activity的四種launchMode

    這篇文章主要介紹了Android開發(fā)中Activity的四種launchMode,launchMode主要用于控制多個Activity間的跳轉(zhuǎn),需要的朋友可以參考下
    2016-03-03
  • Android實現(xiàn)九格智能拼圖算法

    Android實現(xiàn)九格智能拼圖算法

    這篇文章主要為大家詳細介紹了Android實現(xiàn)九格智能拼圖算法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • android動態(tài)壁紙調(diào)用的簡單實例

    android動態(tài)壁紙調(diào)用的簡單實例

    動態(tài)壁紙的實現(xiàn)其實就是在Activity中調(diào)用動態(tài)壁紙服務,通過綁定服務得到IWallpaperService,調(diào)用該接口中的attach函數(shù)實現(xiàn)壁紙的調(diào)用。
    2013-06-06
  • Android編程實現(xiàn)XML解析與保存的三種方法詳解

    Android編程實現(xiàn)XML解析與保存的三種方法詳解

    這篇文章主要介紹了Android編程實現(xiàn)XML解析與保存的三種方法,結合實例形式詳細分析了Android實現(xiàn)xml解析的SAX、DOM、PULL三種方法的相關操作技巧,需要的朋友可以參考下
    2017-08-08
  • Android頁面之間進行數(shù)據(jù)回傳的方法分析

    Android頁面之間進行數(shù)據(jù)回傳的方法分析

    這篇文章主要介紹了Android頁面之間進行數(shù)據(jù)回傳的方法,結合實例形式分析了Android頁面之間進行數(shù)據(jù)的傳遞與處理技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-06-06
  • Android編程重寫ViewGroup實現(xiàn)卡片布局的方法

    Android編程重寫ViewGroup實現(xiàn)卡片布局的方法

    這篇文章主要介紹了Android編程重寫ViewGroup實現(xiàn)卡片布局的方法,實例分析新建FlowLayout繼承ViewGroup類及設置布局文件實現(xiàn)卡片布局效果的相關技巧,需要的朋友可以參考下
    2016-02-02
  • Android 分享功能的實現(xiàn)代碼

    Android 分享功能的實現(xiàn)代碼

    這篇文章主要介紹了Android 分享功能的實現(xiàn)代碼的相關資料,需要的朋友可以參考下
    2017-03-03
  • Android 加載GIF圖最佳實踐方案

    Android 加載GIF圖最佳實踐方案

    最近在項目中遇到需要在界面上顯示一個本地的 GIF 圖的功能,下面通過本文給大家分享Android 加載GIF圖最佳實踐方案,需要的朋友參考下吧
    2017-08-08
  • Flutter控件之實現(xiàn)Widget基類的封裝

    Flutter控件之實現(xiàn)Widget基類的封裝

    在實際的開發(fā)中,Widget的基類還是很有必要存在的,不然就會存在很多的冗余嵌套代碼,本文為大家介紹了Flutter中基類是如何封裝的,需要的可以收藏一下
    2023-05-05

最新評論