国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android組合控件自定義標題欄

2019-10-21 21:35:45
字體:
來源:轉載
供稿:網友

本文實例為大家分享了Android簡單的自定義標題欄,供大家參考,具體內容如下

android自定義控件向來都是開發者最頭疼的,但是我們要有那種迎難而上的精神。

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自定義標題組合控件 步驟: 1.首先寫出需要功能的布局xml,分析布局的父控件是誰? 例如水平布局 父控件應該是linearlayout較為合適 2.創建自定義控件類并繼承xml父控件 3.在構造方法中使用layoutInflat動態加載布局 */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.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;/** * 自定義標題欄 并賦有點擊事件 */public class TitleLayout extends LinearLayout implements View.OnClickListener { private Button btback, btopen; private TextView tvtitle; public TitleLayout(Context context, AttributeSet attrs) {  super(context, attrs);  //動態加載標題欄布局  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) {//監聽點擊事件  switch (view.getId()) {   case R.id.btback:    ((Activity) getContext()).finish();    Toast.makeText(getContext(), "銷毀當前Activity", Toast.LENGTH_SHORT).show();    break;   case R.id.btopen:    Toast.makeText(getContext(), "展開", Toast.LENGTH_SHORT).show();    break;   case R.id.tvtitle:    Toast.makeText(getContext(), "標題", 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>

粘貼以上代碼就可以運行了。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 榆树市| 拜泉县| 象山县| 竹北市| 荆门市| 苍溪县| 白银市| 任丘市| 桐庐县| 徐汇区| 屯昌县| 兴和县| 东方市| 梧州市| 曲松县| 临漳县| 福州市| 兴城市| 光泽县| 双鸭山市| 洛阳市| 诸暨市| 同仁县| 年辖:市辖区| 英山县| 新沂市| 乌恰县| 延津县| 仁化县| 汉阴县| 登封市| 恩施市| 武夷山市| 久治县| 股票| 阿合奇县| 罗平县| 磐石市| 星子县| 洪湖市| 南和县|