您好,登錄后才能下訂單哦!
Android中怎么自定義標(biāo)題欄,針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
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.首先寫出需要功能的布局xml,分析布局的父控件是誰? 例如水平布局 父控件應(yīng)該是linearlayout較為合適 2.創(chuàng)建自定義控件類并繼承xml父控件 3.在構(gòu)造方法中使用layoutInflat動態(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)題欄 并賦有點擊事件 */ public class TitleLayout extends LinearLayout implements View.OnClickListener { private Button btback, btopen; private TextView tvtitle; public TitleLayout(Context context, AttributeSet attrs) { super(context, attrs); //動態(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)聽點擊事件 switch (view.getId()) { case R.id.btback: ((Activity) getContext()).finish(); Toast.makeText(getContext(), "銷毀當(dāng)前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(), "標(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>
關(guān)于Android中怎么自定義標(biāo)題欄問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。