Android組合控件自定義標(biāo)題欄
本文實(shí)例為大家分享了Android簡(jiǎn)單的自定義標(biāo)題欄,供大家參考,具體內(nèi)容如下
android自定義控件向來(lái)都是開(kāi)發(fā)者最頭疼的,但是我們要有那種迎難而上的精神。
MainActivity
package com.example.customview;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
/*
android自定義標(biāo)題組合控件
步驟:
1.首先寫(xiě)出需要功能的布局xml,分析布局的父控件是誰(shuí)?
例如水平布局 父控件應(yīng)該是linearlayout較為合適
2.創(chuàng)建自定義控件類(lèi)并繼承xml父控件
3.在構(gòu)造方法中使用layoutInflat動(dòng)態(tài)加載布局
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//去除自帶標(biāo)題欄
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
}
}
TitleLayout.class
package com.example.customview.custom;
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.TextView;
import android.widget.Toast;
import com.example.customview.R;
/**
* 自定義標(biāo)題欄 并賦有點(diǎn)擊事件
*/
public class TitleLayout extends LinearLayout implements View.OnClickListener {
private Button btback, btopen;
private TextView tvtitle;
public TitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
//動(dòng)態(tài)加載標(biāo)題欄布局
LayoutInflater.from(context).inflate(R.layout.custom_layout, this);
initView();
}
private void initView() {//初始化控件
btback = (Button) findViewById(R.id.btback);
btback.setOnClickListener(this);
btopen = (Button) findViewById(R.id.btopen);
btopen.setOnClickListener(this);
tvtitle = (TextView) findViewById(R.id.tvtitle);
tvtitle.setOnClickListener(this);
}
@Override
public void onClick(View view) {//監(jiān)聽(tīng)點(diǎn)擊事件
switch (view.getId()) {
case R.id.btback:
((Activity) getContext()).finish();
Toast.makeText(getContext(), "銷(xiāo)毀當(dāng)前Activity", Toast.LENGTH_SHORT).show();
break;
case R.id.btopen:
Toast.makeText(getContext(), "展開(kāi)", Toast.LENGTH_SHORT).show();
break;
case R.id.tvtitle:
Toast.makeText(getContext(), "標(biāo)題", Toast.LENGTH_SHORT).show();
break;
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.customview.MainActivity"> <include layout="@layout/custom_layout" /> <com.example.customview.custom.TitleLayout android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
custom_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.customview.MainActivity"> <include layout="@layout/custom_layout" /> <com.example.customview.custom.TitleLayout android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
粘貼以上代碼就可以運(yùn)行了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android源碼系列之深入理解ImageView的ScaleType屬性
Android源碼系列第一篇,這篇文章主要從源碼的角度深入理解ImageView的ScaleType屬性,感興趣的小伙伴們可以參考一下2016-06-06
Android Canvas之drawBitmap方法案例詳解
這篇文章主要介紹了Android Canvas之drawBitmap方法案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Android在一個(gè)app中安裝并卸載另一個(gè)app的示例代碼
這篇文章主要介紹了Android在一個(gè)app中安裝并卸載另一個(gè)app的示例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
Android使用ViewPager實(shí)現(xiàn)啟動(dòng)引導(dǎo)頁(yè)
這篇文章主要為大家詳細(xì)介紹了Android使用ViewPager實(shí)現(xiàn)第一次啟動(dòng)引導(dǎo)頁(yè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07
Android實(shí)現(xiàn)點(diǎn)擊某個(gè)按鈕指定位置彈出布局
這篇文章主要介紹了Android實(shí)現(xiàn)點(diǎn)擊某個(gè)按鈕指定位置彈出布局,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12
Android實(shí)現(xiàn)滑動(dòng)加載數(shù)據(jù)的方法
這篇文章主要介紹了Android實(shí)現(xiàn)滑動(dòng)加載數(shù)據(jù)的方法,實(shí)例分析了Android通過(guò)滑動(dòng)實(shí)現(xiàn)動(dòng)態(tài)加載數(shù)據(jù)的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07

