【Android學習】標題欄titleBar
阿新 • • 發佈:2019-01-24
1,去掉標題欄
1)方法一:在清單檔案中修改主題
activity或application標籤中:
android:theme=”@android:style/Theme.Light.NoTitleBar
2)方法二:使用style
在清單檔案(manifest.xml)裡面:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style /Theme_NoTitleBar" >//這句
在style.xml中新增:
<!-- 沒有標題欄 -->
<style name="Theme_NoTitleBar">
<item name="android:windowNoTitle">true</item>
</style>
3)Android Studio方法
在Android Studio中用以上的方法無法去掉標題欄。
原因:這裡的Activity類繼承的是AppCompatActivity,這個是屬於support v7附加包的類。而eclipse使用的則是v4包,繼承的是Activity類。
解決方案:
①(不推薦方案):修改父類Acticity,將AppComPatActiviry替換為Activiry,問題解決
②修改AndroidManiFest.xml檔案,將< activity>中的Theme屬性替換如下:
android:theme="@style/Theme.AppCompat.NoActionBar">
③換requestWindowFeature(Window.FEATURE_NO_TITLE);
//v7包下去除標題欄程式碼:
getSupportActionBar().hide();
getWindow().setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
2,自定義標題欄
實現的自定義的標題欄,中間是TextView,用來寫標題。然後左邊一個按鈕右邊一個按鈕。點選下載原始碼
1)首先,重寫一個類TitleBarView,自定義標題欄樣式。
package com.luo.view;
import com.example.layout_demo.R;
import com.luo.util.SystemMethod;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class TitleBarView extends RelativeLayout {
private Context mContext;
private Button btn_Left;
private Button btn_Right;
private TextView tv_center;
public TitleBarView(Context context) {
super(context);
mContext = context;
initView();
}
public TitleBarView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
initView();
}
private void initView() {
LayoutInflater.from(mContext).inflate(R.layout.common_title_bar, this);
btn_Left = (Button) findViewById(R.id.title_btn_left);
btn_Right = (Button) findViewById(R.id.title_btn_right);
tv_center = (TextView) findViewById(R.id.title_txt);
}
public void setCommonTitle(int LeftVisibility, int centerVisibility, int rightVisibility) {
btn_Left.setVisibility(LeftVisibility);
btn_Right.setVisibility(rightVisibility);
tv_center.setVisibility(centerVisibility);
}
/**
* 左按鈕文字
*
* @param txtRes
*/
public void setbtn_LeftText(int txtRes) {
btn_Left.setText(txtRes);
}
/**
* 右按鈕文字
*
* @param txtRes
*/
public void setbtn_RightText(int txtRes) {
btn_Right.setText(txtRes);
}
/**
* 左按鈕是圖片
*
* @param icon
*/
public void setbtn_LeftDrawble(int icon) {
Drawable img = mContext.getResources().getDrawable(icon);
int height = SystemMethod.dip2px(mContext, 30);
int width = img.getIntrinsicWidth() * height / img.getIntrinsicHeight();
img.setBounds(0, 0, width, height);
btn_Left.setCompoundDrawables(img, null, null, null);
}
/**
* 右按鈕是圖片
*
* @param icon
*/
public void setbtn_RightDrawble(int icon) {
Drawable img = mContext.getResources().getDrawable(icon);
int height = SystemMethod.dip2px(mContext, 30);
// .getIntrinsicWidth():獲取Drawable image物件的寬度
int width = img.getIntrinsicWidth() * height / img.getIntrinsicHeight();
/*
* setBounds(x,y,width,height); x:元件在容器X軸上的起點 y:元件在容器Y軸上的起點 width:元件的長度
* height:元件的高度
*/
img.setBounds(0, 0, width, height);
btn_Right.setCompoundDrawables(img, null, null, null);
}
/**
* 中間標題文字
*
* @param txtRes
*/
public void setTitleText(String txtRes) {
tv_center.setText(txtRes);
}
public void setbtn_LeftOnclickListener(OnClickListener listener) {
btn_Left.setOnClickListener(listener);
}
public void setbtn_RightOnclickListener(OnClickListener listener) {
btn_Right.setOnClickListener(listener);
}
public void destoryView() {
btn_Left.setText(null);
btn_Right.setText(null);
tv_center.setText(null);
}
}
對應common_title_bar.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="@color/dark_orange"
android:orientation="vertical">
<TextView
android:id="@+id/title_top"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="@null"/>
<RelativeLayout
android:id="@+id/title_bar"
android:layout_height="50dp"
android:layout_width="match_parent">
<Button
android:id="@+id/title_btn_left"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:textColor="@color/blue"
android:textSize="16sp"
android:background="@color/dark_orange"/>
<TextView
android:id="@+id/title_txt"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_centerInParent="true"
android:gravity="center"
android:textSize="@dimen/textSize"
android:textColor="@color/blue"
android:textStyle="bold"/>
<Button
android:id="@+id/title_btn_right"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="@color/dark_orange"/>
</RelativeLayout>
</LinearLayout>
2)用的時候,呼叫自定義函式setTitle()設定:
private void setTitle() {
mTitleBarView = (TitleBarView) findViewById(R.id.act_main_titleBar);
mTitleBarView.setCommonTitle(View.VISIBLE, View.VISIBLE, View.VISIBLE);
mTitleBarView.setbtn_RightDrawble(R.drawable.icon_message);
mTitleBarView.setbtn_LeftDrawble(R.drawable.icon_qr_code);
mTitleBarView.setTitleText("淘寶搜尋框");
mTitleBarView.setbtn_RightOnclickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast("我是右按鈕");
}
});
mTitleBarView.setbtn_LeftOnclickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast("我是左按鈕");
}
});
}
3,Translucent Bars(透明欄,沉浸式頂欄)
即頂欄透明,由app填充。
使用者體驗更好,更美觀。
點選下載原始碼
1)在對應的activity的.xml檔案中,新增TextView用來填充透明的頂欄。
<?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="@color/dark_orange"
android:orientation="vertical">
<TextView
android:id="@+id/title_top"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="@null"/>
</LinearLayout>
2)activity的onCreate()中,呼叫setTopTitle()函式。
/**
* 設定Translucent Bars
*/
private void setTopTitle(){
//透明狀態列
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//透明導航欄
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//設定狀態列填充
TextView tv_title_top = (TextView) findViewById(R.id.title_top);
tv_title_top.setHeight( SystemMethod.getStatusHeight(this));
}
其中的getStatusHeight是一個獲取狀態列高度的函式。
/**
* 獲得狀態列高度
* @param context
* @return
*/
public static int getStatusHeight(Context context) {
int statusHeight = -1;
try {
Class clazz = Class.forName("com.android.internal.R$dimen");
Object object = clazz.newInstance();
int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
statusHeight = context.getResources().getDimensionPixelSize(height);
} catch (Exception e) {
e.printStackTrace();
}
return statusHeight;
}
問題:以上方法可能導致狀態列高度設定的比實際高度高。
改進方案如下:
public static void setSystemStatusHeight(Context context, View view){
LinearLayout.LayoutParams sp_params = new LinearLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
sp_params.height = SystemMethod.getStatusHeight(context);
view.setLayoutParams(sp_params);
view.setVisibility(View.VISIBLE);
}