㈠ android使用fragment實現底部導航欄切換界面
源碼鏈接
效果圖
創建bottom_layout.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="match_parent"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_width="36dp"
android:layout_height="36dp"
android:id="@+id/bottom_icon"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#565656"
android:textSize="10sp"
android:id="@+id/bottom_text" />
public void setNormalImage(int normalIcon){
this.normalIcon = normalIcon;
ivIcon.setImageResource(normalIcon);
}
public void setFocusedImage(int focusedIcon){
this.focusedIcon = focusedIcon;
}
public void setTvText(String text){
tvText.setText(text);
}
public void setFocused(boolean isFocused){
this.isFocused = isFocused;
if(isFocused){
ivIcon.setImageResource(focusedIcon);
tvText.setTextColor(Color.parseColor("#02b5bc"));
}else{
ivIcon.setImageResource(normalIcon);
tvText.setTextColor(Color.BLACK);
}
}
<FrameLayout
android:id="@+id/frameLayout_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="11"
>
</FrameLayout>
四個如下
<com.example.qiaolulu.qiaofragment.BottomLayout
android:id="@+id/square"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
</com.example.qiaolulu.qiaofragment.BottomLayout>
public class Babyextends Fragment{
@Nullable
@Override
public ViewonCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//載入你設計要顯示的界面
View view = inflater.inflate(R.layout.baby,null);
return view;
}
}
fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction =fragmentManager.beginTransaction();
firstPage =new FirstPage();
transaction.add(R.id.frameLayout_container,firstPage);
transaction.commit();
㈡ android 判斷有沒有底部導航欄
很多android應用底部都有一個底部導航欄,方便用戶在使用過程中隨意切換。目前常用的做法有三種:一種是使用自定義tabHost,一種是使用activityGroup,一種是結合FrameLayout實現。筆者再做了多款應用後,為了節約開發周期,封裝了一個抽象類,只要三步便可完成底部欄的生成及不同頁面的調用。
public class extends ActivityCollection {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setBottomTabBackground(resId);// 設置底部導航背景圖
@Override
protected boolean isShowWindowFeature() {
return true;//設置是否顯示title;
@Override
protected ListIndicatorInfo> setDrawableCollections() {
ListIndicatorInfo> IndicatorInfos = new ArrayListIndicatorInfo>();
IndicatorInfo indicatorInfo_1 = new IndicatorInfo(R.drawable.baby1,
R.drawable.baby1_s, R.string.baby1, 12, Color.WHITE,
new Intent(.this,
Activity01.class));
IndicatorInfo indicatorInfo_2 = new IndicatorInfo(R.drawable.baby2,
R.drawable.baby2_s, R.string.baby2, 12, Color.WHITE,
new Intent(.this,
Activity02.class));
IndicatorInfo indicatorInfo_3 = new IndicatorInfo(R.drawable.baby3,
R.drawable.baby3_s, R.string.baby3, 12, Color.WHITE,
new Intent(.this,
Activity03.class));
IndicatorInfo indicatorInfo_4 = new IndicatorInfo(R.drawable.baby4,
R.drawable.baby4_s, R.string.baby4, 12, Color.WHITE,
new Intent(.this,
Activity04.class));
IndicatorInfos.add(indicatorInfo_1);
IndicatorInfos.add(indicatorInfo_2);
IndicatorInfos.add(indicatorInfo_3);
IndicatorInfos.add(indicatorInfo_4);
return IndicatorInfos;
第一步:導入jar包;
第二步:讓你的homeactivity 繼承ActivityCollection類;
第三步:將你的圖片資源及跳轉intent放入list中,設置可選項;
雛形就形成啦!
㈢ 超簡單,幾行代碼搞定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開發時如何去掉底部的導航欄
在一個普通類中(非繼承Activity等),點擊截界面上某一按鈕隱藏底部導航欄(Back、Home、多任務切換),再次點擊讓其出現;
實現:隱藏view.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| Utils.SYSTEM_UI_FLAG_IMMERSIVE);
顯示view.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
而且加了界面touch監聽,在隱藏模式下,單擊屏幕不讓導航欄出現;