android導航條
① Android 顯示、隱藏狀態欄和導航欄
Android 顯示、隱藏狀態欄和導航欄
控制狀態欄顯示,Activity的主題中配置全屏屬性
控制狀態欄顯示,在setContentView之前設置全屏的flag
控制狀態欄顯示,在任何位置通過添加和移除全屏的flag
控制狀態欄和導航欄顯示,setSystemUiVisibility
// 全屏展示
// 非全屏顯示,顯示狀態欄和導航欄
② 超簡單,幾行代碼搞定Android底部導航欄
咳咳,答應過年增加新功能的,沒想到拖到現在,延遲了一個來月,尷尬,尷尬
那個,我們先忽略這尷尬的事情吧,進入正題才是最重要滴
老規矩,先上效果圖:
跟原來的圖有個很明顯的區別,你們也一定都發現了,對不對。那麼顯眼的小紅點,一定都看到了吧。
當然除了這個,還增加了一項功能,雖然不是很明顯,但相信也有小夥伴發現了吧,截圖的這倆手機屏幕明顯大小不同,但是底部導航欄的大小還是相差不大滴。
是的,你們沒有看多,這次不僅增加了小紅點功能,還增加了底部導航欄的適配,你沒有聽錯,以後底部導航欄也不用那些dp、sp了,都按照UI妹子們標注的px來就可以了,再也不用為了底部導航欄去跟UI妹子解釋啥叫dp了。
好了,效果圖展示完了,現在該進入枯燥的使用介紹了。
由於這次改動有點大,所以,先介紹下上個穩定版本的用法,到底是用最新的,還是用原來的,就看各位小夥伴的意願了
上個穩定版本是1.1.3的,引用方式如下
compile 'com.hjm:BottomTabBar:1.1.3'
具體用法如下(備注都加好了,我也就不多廢話了):
最新版本是1.2.2的,引用方式如下
compile 'com.hjm:BottomTabBar:1.2.2'
其實1.2.0與1.1.3區別並不大,只有4點改動:
現在默認的,分割線高度都是設置的1個像素。這里以後也固定都用這個默認的高度了,不再對外提供修改的方法。
這就是新增加的適配了,多的也不說了,你們都懂的
標准尺寸,就是UI妹子給你提供的效果圖的屏幕尺寸,只要在init()方法里添加上標准尺寸,你就可以放肆的使用px了
這個方法就是控制小紅點顯示的方法了,index就是需要顯示或者隱藏小紅點的TabItem,isShow是一個boolean類型的參數,他是控制小紅點是否顯示的,如果為true,就會顯示小紅點;如果為false,就會隱藏小紅點
1.2.2版本新增了兩個方法
介紹到這里,超簡單的底部導航欄,第二階段就可以告一段落了。以後還會持續優化,完善的。
第三階段我打算封裝一下有中間凸起的底部導航欄,這個功能我本地已經做了,但是封裝進去的時候,封裝的不理想,這次就沒有上線,留作下次了。
最後,再上個 GitHub 地址
③ 如何隱藏/顯示 android 系統的虛擬導航欄/按鍵
安卓 4.4 以上的版本支持隱藏導航欄,也就是那三個虛擬的按鍵,本篇經驗就介紹如何來隱藏著三個按鍵。
工具/原料
安卓4.4 以上版本的手機
方法/步驟
安卓4.4以上版本,增加了虛擬的三個按鍵,谷歌稱此三個按鈕為導航欄,這三個導航欄是可以隱藏起來的,以節省更多的屏幕空間。
點擊設置,進入手機設置。
步驟閱讀
在設置中,找到「導航欄可以隱藏」的設置項,將其開啟。
開啟後,立即生效,點擊返回。可以發現導航欄左邊多了一個V形的隱藏按鈕。點擊它即可隱藏導航欄。
隱藏後的效果。
步驟閱讀
如果需要讓導航欄再次出現,只需在屏幕底部,向上滑動
向上滑動之後,導航欄就再次顯示出來。
④ 如何實現Android透明導航欄
實現功能
1.步驟:
1) 創建一個工程,主布局就先做一個ImageView,自己找個好看的圖片做src。
2) 在Activity重寫的onCreate方法中獲得窗口視圖對象(DecorView)
3) 設置DecorView的SystemUiVisibility
4) 設置導航條、狀態欄的顏色–>透明
5) 獲取當前Activity的ActionBar並隱藏
2.具體代碼和注釋:
獲取DecorView對象
java">@Override
protectedvoidonCreate(BundlesavedInstanceState){
...
ViewdecorView=getWindow().getDecorView();
...
}
設置SystemUiVisibility
intoption=View.SYSTEM_UI_FLAG_FULLSCREEN//全屏標記
|View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN//布局全屏標記,避免退出全屏模式時內容被覆蓋
|View.SYSTEM_UI_FLAG_HIDE_NAVIGATION//隱藏導航欄標記
|View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION//布局隱藏導航欄標記,同理
|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY//粘性沉浸體驗
|View.SYSTEM_UI_FLAG_LAYOUT_STABLE;//確保上述標記穩定//此方法用來設置系統UI的可見性,系統UI包括狀態欄、ActionBar、導航欄devorView.setSystemUiVisibility(option);
設置狀態欄、導航欄的顏色:
getWindow().setStatusBarColor(Color.TRANSPARENT);//Color.TRANSPARENT=0表示#00000000即透明顏色
getWindow().setNavigationBarColor(Color.TRANSPARENT);
獲取本頁面的ActionBar並隱藏起來
ActionBaractionBar=getSupportActionBar();//注意:此處用的Activity繼承的是
AppCompatActivity(它繼承的是FragmentActivity)
//所以調用的是getSupport...方法,如果繼承Activity則直接調用get...方法
assertactionBar!=null;//這一句可以不理會,反正我是Ctrl+F1提示出來的,意思其實是判斷如果actionBar不為空則向下執行。
actionBar.hide();
注意:最後一點注意事項是:只支持Android API 21以上的手機
⑤ android關掉導航欄後怎麼
1.
在手機桌面上找到【設置】按鈕,點擊「設置」去設置界面找回
2.
點擊「設置」後,進入「常用設置」,在右邊是「全部設置」,點擊「全部設置」,屏幕往上翻,在下面找到「導航欄」;
3.
選擇並點擊「導航欄」進入「導航欄」頁面,這里可以看到「導航欄可隱藏」後面的按鈕呈開啟狀態,原因就出在這里;
4.
將「導航欄可隱藏」後面的按鈕呈關閉
⑥ Android自定義字母導航欄
自定義側邊字母導航欄,根據實際字母高度進行顯示
先上效果圖
public class SlideBar extends View {
//當前手指滑動到的位置
private int choosedPosition = -1;
//畫文字的畫筆
private Paint paint;
//單個字母的高度
private float perTextHeight;
//字母的字體大小
private float letterSize;
//字母的垂直間距
private float letterGap;
//字母圓形背景半徑
private float bgRadius;
private ArrayList<String> firstLetters = new ArrayList<>();
//繪制點擊時的藍色背景
private Paint backgroundPaint;
private Context context;
private OnTouchFirstListener listener;
public RecyclerView getTiku_recycle_answer() {
return tiku_recycle_answer;
}
public void setTiku_recycle_answer(RecyclerView tiku_recycle_answer) {
this.tiku_recycle_answer = tiku_recycle_answer;
}
RecyclerView tiku_recycle_answer;
public SlideBar(Context context) {
this(context, null);
}
public SlideBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SlideBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SlideBar);
//字母的字體大小
letterSize = typedArray.getDimension(R.styleable.SlideBar_letter_size, DisplayUtils.sp2px(context, 10.0f));
//每個字母的高
perTextHeight = typedArray.getDimension(R.styleable.SlideBar_letter_height, DisplayUtils.dp2px(context, 10.0f));
//字母垂直間距
letterGap = typedArray.getDimension(R.styleable.SlideBar_letter_gap, DisplayUtils.dp2px(context, 6.0f));
//字母垂直間距
bgRadius = typedArray.getDimension(R.styleable.SlideBar_letter_bg_radius, DisplayUtils.dp2px(context, 8.0f));
typedArray.recycle();
init();
}
public void init() {
//初始化畫筆
paint = new Paint();
paint.setAntiAlias(true);
paint.setTextSize(letterSize);
paint.setTypeface(Typeface.DEFAULT_BOLD);
//初始化圓形背景畫筆
backgroundPaint = new Paint();
backgroundPaint.setAntiAlias(true);
backgroundPaint.setColor(context.getResources().getColor(R.color.color_368FFF));
}
public void setFirstLetters(ArrayList<String> letters) {
firstLetters.clear();
firstLetters.addAll(letters);
invalidate();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec); //獲取寬的模式
int heightMode = MeasureSpec.getMode(heightMeasureSpec); //獲取高的模式
int widthSize = MeasureSpec.getSize(widthMeasureSpec); //獲取寬的尺寸
int heightSize = MeasureSpec.getSize(heightMeasureSpec); //獲取高的尺寸
int width = 0;
int height;
if (widthMode == MeasureSpec.EXACTLY) {
//如果match_parent或者具體的值,直接賦值
width = widthSize;
} else {
//如果其他模式,則指定一個寬度
width = DisplayUtils.dp2px(getContext(), 20.0f);
}
//高度跟寬度處理方式一樣
if (heightMode == MeasureSpec.EXACTLY) {
height = heightSize;
} else {
float textHeight = perTextHeight;
height = (int) (getPaddingTop() + textHeight * (firstLetters.size() + 1) + letterGap * (firstLetters.size() - 1) + getPaddingBottom());
}
if (height > tiku_recycle_answer.getMeasuredHeight()) {
height = tiku_recycle_answer.getMeasuredHeight();
}
//保存測量寬度和測量高度
setMeasuredDimension(width, height);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int i = 0; i < firstLetters.size(); i++) {
paint.setColor(i == choosedPosition ? Color.WHITE : context.getResources().getColor(R.color.color_368FFF));
float x = (getWidth() - paint.measureText(firstLetters.get(i))) / 2;
float y = (float) getHeight() / firstLetters.size();//每個字母的高度
if (i == choosedPosition) {
canvas.drawCircle((float) (getWidth() / 2), i * y + y / 2, bgRadius, backgroundPaint);
}
//垂直位置需要增加一個偏移量,上移兩個像素,因為根據計算得到的是baseline,將字母上移,使其居中在圓內
canvas.drawText(firstLetters.get(i), x, (perTextHeight + y) / 2 + y * i-2, paint);
}
}
//觸碰事件
//按下,松開,拖動
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
this.setBackgroundColor(context.getResources().getColor(android.R.color.transparent));
float y = event.getY();
//獲取觸摸到字母的位置
choosedPosition = (int) y * firstLetters.size() / getHeight();
//上滑超過邊界,顯示第一個
if (choosedPosition < 0) {
choosedPosition = 0;
}
//下滑超過邊界,顯示最後一個
if (choosedPosition >= firstLetters.size()) {
choosedPosition = firstLetters.size() - 1;
}
if (listener != null) {
//滑動A-Z字母聯動外層數據
listener.onTouch(firstLetters.get(choosedPosition));
}
break;
case MotionEvent.ACTION_UP:
this.setBackgroundColor(context.getResources().getColor(android.R.color.transparent));
choosedPosition = -1;
if (listener != null) {
//滑動A-Z字母聯動外層數據
listener.onRelease();
}
break;
}
//重繪
invalidate();
return true;
}
public void setFirstListener(OnTouchFirstListener listener) {
this.listener = listener;
}
/**
* OnTouchFirstListener 介面
* onTouch:觸摸到了那個字母
* onRelease:up釋放時中間顯示的字母需要設置為GONE
*/
public interface OnTouchFirstListener {
void onTouch(String firstLetter);
void onRelease();
}
}
<declare-styleable name="SlideBar">
<attr name="letter_size" format="dimension" />
<attr name="letter_height" format="dimension" />
<attr name="letter_gap" format="dimension" />
<attr name="letter_bg_radius" format="dimension" />
</declare-styleable>
xml中引入,我的是constraintlayout,具體設置看自己的布局
<com.answer.view.SlideBar
android:id="@+id/slideBar"
android:layout_width="@dimen/dp_20"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@+id/tiku_recycle_answer"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guide_answer"
app:layout_constraintTop_toTopOf="@+id/tiku_recycle_answer"
app:letter_bg_radius="@dimen/dp_8"
app:letter_gap="@dimen/dp_6"
app:letter_height="@dimen/dp_10"
app:letter_size="@dimen/sp_10" />
private void handleSlideBarEvent() {
List<QuesCommentSubjectiveStuBean> datas = .getDatas();//獲取處理後的數據,賦值給導航欄
ArrayList<String> letters = new ArrayList<>();
for (QuesCommentSubjectiveStuBean stuBean : datas) {
if (letters.contains(stuBean.getFirstLetter())) {
continue;
}
letters.add(stuBean.getFirstLetter());
}
slideBar.setFirstLetters(letters);
slideBar.setTiku_recycle_answer(tiku_recycle_answer);
slideBar.setFirstListener(new SlideBar.OnTouchFirstListener() {
@Override
public void onTouch(String firstLetter, float dy) {
tv_first_letter.setVisibility(VISIBLE);
tv_first_letter.setText(firstLetter);
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) tv_first_letter.getLayoutParams();
//如果是第一個字母,修改提示框顯示位置
layoutParams.topMargin = (int) dy + slideBar.getTop() - tv_first_letter.getMeasuredHeight() / 2;
//異常情況,點擊最後一個字元,提示框顯示不全的場景,如果顯示位置超過屏幕,則靠底部顯示
if ((int) dy + slideBar.getTop() + tv_first_letter.getMeasuredHeight() / 2 > tiku_recycle_answer.getBottom()) {
layoutParams.topMargin = tiku_recycle_answer.getBottom() - tv_first_letter.getMeasuredHeight();
}
tv_first_letter.setLayoutParams(layoutParams);
//滑動後移動到對應的位置,找到第一個匹配到首字母的學生,位移到此處
int newPosition = -1;
for (QuesCommentSubjectiveStuBean stuBean : datas) {
if (firstLetter.equals(stuBean.getFirstLetter())) {
newPosition = datas.indexOf(stuBean);
break;
}
}
//move時會多次觸發,此處只響應第一次
if (newPosition != lastPosition) {
lastPosition = newPosition;
Lg.d(TAG, "questionComment-->--滑動導航欄跳轉到首字母:" + firstLetter);
subJectLinearLayoutManager.scrollToPositionWithOffset(lastPosition, 0);
}
}
@Override
public void onRelease() {
postDelayed(new Runnable() {
@Override
public void run() {
lastPosition = -1;
tv_first_letter.setVisibility(GONE);
}
}, 200);
}
});
}
5.一個小問題。
用於放大顯示選中字母的TextView在布局中,請設置為invisible,這樣在載入xml布局時,會對這個控制項進行測量和布局,只是不顯示,這樣我們才能獲得tv_first_letter.getMeasuredHeight(),如果設置為gone,不會進行測量,獲取的高度就為0,這樣在第一次顯示的時候就會有一個顯示位置跳動的異常。設置為invisible就可以解決這個問題,目的就是讓系統測量一下TextView的寬高,不想這么搞的話,在第4步之前手動測量一次也是可以的。
<TextView
android:id="@+id/tv_first_letter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/dp_2"
android:background="@mipmap/ic_bubble"
android:fontFamily="sans-serif"
android:gravity="center"
android:text="C"
android:textColor="@color/color_ffffff"
android:textSize="@dimen/sp_18"
android:visibility="invisible"
app:layout_constraintEnd_toStartOf="@+id/guide_answer"
app:layout_constraintTop_toTopOf="parent" />
⑦ 如何實現Android透明導航欄
1、androidkitkat 有一個新的特性可以設置手機狀態欄的背景,讓手機整個界面的風格保持一致,看起來非常清爽。
2、android4.4提供了一套能透明的系統ui樣式給狀態欄和導航欄,這樣的話就不用向以前那樣每天面對著黑乎乎的上下兩條黑欄了,還可以調成跟activity 一樣的樣式,形成一個完整的主題。
3、首先要打開activity的透明主題功能,可以把activity的主題設置繼承*.TranslucentDecor 主題,然後設置android:windowTranslucentNavigation或者android:windowTranslucentStatus的主題屬性為true,又或者在activity的代碼裡面開啟FLAG_TRANSLUCENT_NAVIGATION或是FLAG_TRANSLUCENT_STATUS的window窗口標識。由於透明主題不能在4.4以前的版本裡面使用,所以系統樣式跟以前沒有區別,也就是看不到任何變化,這是一個兼容模式,這個模式可以兼容到api 10。

