Android實現頂部底部雙導航介面功能
最近想弄一個雙導航功能,查看了許多資料,總算是實現了功能,這邊就算是給自己幾個筆記吧!
先來看看效果
那麼就開始實現了!
底部導航欄我選擇用FragmentTabHost+Fragment來實現,這個方法我覺得挺好用的,程式碼量也不多
首先是開始的activity_main.xml
?12345678910111213141516171819202122232425 | android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = "${relativePackage}.${activityClass}" > < FrameLayout android:id = "@+id/main_view" android:layout_width = "match_parent" android:layout_height = "match_parent" android:layout_above = "@+id/main_tab" android:layout_alignParentLeft = "true" android:layout_alignParentTop = "true" > </ FrameLayout > < view android:id = "@+id/main_tab" android:layout_width = "match_parent" android:layout_height = "50dp" android:layout_alignParentBottom = "true" android:layout_alignParentLeft = "true" class = "android.support.v4.app.FragmentTabHost" /> </ RelativeLayout > |
其中我是直接拉的view所以是形成的FragmentTabHost
也可以直接在xml檔案裡面寫
<android.support.v4.view.FragmentTabHost >
< /android.support.v4.view.FragmentTabHost>
再來是tab_foot.xml
1234567891011121314151617181920212223 | <? xml version = "1.0" encoding = "utf-8" ?> android:layout_width = "match_parent" android:layout_height = "match_parent" android:background = "#F6F6F6" android:gravity = "center" android:orientation = "vertical" > < ImageView android:id = "@+id/foot_iv" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:src = "@drawable/home1" /> < TextView android:id = "@+id/foot_tv" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_marginTop = "3dp" android:text = "首頁" android:textColor = "@color/tab_color" /> </ LinearLayout > |
這是每個底部按鈕的佈局設定的xml檔案
顯示效果。
再來是MainActivity的程式碼
?1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 | package com.gjn.mynavigation; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTabHost; import android.view.LayoutInflater; import android.view.View; import android.view.Window; import android.widget.ImageView; import android.widget.TabWidget; import android.widget.TextView; import android.widget.TabHost.OnTabChangeListener; import android.widget.TabHost.TabSpec; public class MainActivity extends FragmentActivity implements OnTabChangeListener { private FragmentTabHost mTabHost; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); //初始化FragmentTabHost initHost(); //初始化底部導航欄 initTab(); //預設選中 mTabHost.onTabChanged(TabDb.getTabsTxt()[ 0 ]); } private void initTab() { String[] tabs = TabDb.getTabsTxt(); for ( int i = 0 ; i < tabs.length; i++) { //新建TabSpec TabSpec tabSpec = mTabHost.newTabSpec(TabDb.getTabsTxt()[i]); //設定view View view = LayoutInflater.from( this ).inflate(R.layout.tabs_foot, ); ((TextView) view.findViewById(R.id.foot_tv)).setText(TabDb.getTabsTxt()[i]); ((ImageView) view.findViewById(R.id.foot_iv)).setImageResource(TabDb.getTabsImg()[i]); tabSpec.setIndicator(view); //加入TabSpec mTabHost.addTab(tabSpec,TabDb.getFramgent()[i], ); } } /*** * 初始化Host */ private void initHost() { mTabHost = (FragmentTabHost) findViewById(R.id.main_tab); //呼叫setup方法 設定view mTabHost.setup( this , getSupportFragmentManager(),R.id.main_view); //去除分割線 mTabHost.getTabWidget().setDividerDrawable( ); //監聽事件 mTabHost.setOnTabChangedListener( this ); } @Override public void onTabChanged(String arg0) { //從分割線中獲得多少個切換介面 TabWidget tabw = mTabHost.getTabWidget(); for ( int i = 0 ; i < tabw.getChildCount(); i++) { View v = tabw.getChildAt(i); TextView tv = (TextView) v.findViewById(R.id.foot_tv); ImageView iv = (ImageView) v.findViewById(R.id.foot_iv); //修改當前的介面按鈕顏色圖片 if (i == mTabHost.getCurrentTab()) { tv.setTextColor(getResources().getColor(R.color.tab_light_color)); iv.setImageResource(TabDb.getTabsImgLight()[i]); } else { tv.setTextColor(getResources().getColor(R.color.tab_color)); iv.setImageResource(TabDb.getTabsImg()[i]); } } } } |
其中TabDb類是用來設定導航欄的資料和圖片切換時候的資源
以下是TabDb類
1234567891011121314151617181920212223242526272829303132 | package com.gjn.mynavigation; public class TabDb { /*** * 獲得底部所有項 */ public static String[] getTabsTxt() { String[] tabs = { "首頁" , "交易" , "地點" , "我的" }; return tabs; } /*** * 獲得所有碎片 */ public static Class[] getFramgent(){ Class[] cls = {OneFm. class ,TwoFm. class ,ThreeFm. class ,FourFm. class }; return cls ; } /*** * 獲得所有點選前的圖片 */ public static int [] getTabsImg(){ int [] img = {R.drawable.home1,R.drawable.glod1,R.drawable.xc1,R.drawable.user1}; return img ; } /*** * 獲得所有點選後的圖片 */ public static int [] getTabsImgLight(){ int [] img = {R.drawable.home2,R.drawable.glod2,R.drawable.xc2,R.drawable.user2}; return img ; } } |
到此,底部導航欄就算是完全實現了。
————————————————————————————————————————–
現在來實現頂部導航欄,看了許多最後使用了RadioGroup+ViewPager來實現
首先是為第一個碎片設計一個xml佈局
fm_one.xml
?12345678910111213141516171819202122232425262728 | <? xml version = "1.0" encoding = "utf-8" ?> android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" > < HorizontalScrollView android:id = "@+id/one_hv" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:scrollbars = "none" > < RadioGroup android:id = "@+id/one_rg" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "horizontal" > </ RadioGroup > </ HorizontalScrollView > < view android:id = "@+id/one_view" android:layout_width = "match_parent" android:layout_height = "0dp" android:layout_weight = "1" class = "android.support.v4.view.ViewPager" /> </ LinearLayout > |
設定頂部導航欄和顯示view
之後吧導航欄的每個項的佈局
tab_rb.xml
?123456789101112 | <? xml version = "1.0" encoding = "utf-8" ?> android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:background = "@drawable/tab_rb_selector" android:button = "@null" android:paddingBottom = "10dp" android:paddingLeft = "15dp" android:paddingRight = "15dp" android:paddingTop = "10dp" android:text = "今日" > </ RadioButton > |
其中設定selector檔案來控制點選和未點選的狀態
tab_rb_selector.xml
?123456789101112131415161718192021222324 | <? xml version = "1.0" encoding = "utf-8" ?> <!-- 點選 --> < item android:state_checked = "true" > < layer-list > < item > < shape android:shape = "rectangle" > < stroke android:width = "5dp" android:color = "@color/tab_light_color" /> </ shape > </ item > < item android:bottom = "5dp" > < shape android:shape = "rectangle" > < solid android:color = "#fff" /> </ shape > </ item > </ layer-list > </ item > <!-- 預設 --> < item > < shape > < solid android:color = "#fafafa" /> </ shape > </ item > </ selector > |
設定了點選和預設的時候的顯示狀態
最後來實現OneFm類
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 | package com.gjn.mynavigation; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.util.DisplayMetrics; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.HorizontalScrollView; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.LayoutParams; import android.widget.RadioGroup.OnCheckedChangeListener; public class OneFm extends Fragment implements OnPageChangeListener { private View view; private RadioGroup rg_; private ViewPager vp_; private HorizontalScrollView hv_; private List<Fragment> newsList = new ArrayList<Fragment>(); private OneFmAdapter adapter; @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { if (view == ) { //初始化view view = inflater.inflate(R.layout.fm_one, container, false ); rg_ = (RadioGroup) view.findViewById(R.id.one_rg); vp_ = (ViewPager) view.findViewById(R.id.one_view); hv_ = (HorizontalScrollView) view.findViewById(R.id.one_hv); //設定RadioGroup點選事件 rg_.setOnCheckedChangeListener( new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int id) { vp_.setCurrentItem(id); } }); //初始化頂部導航欄 initTab(inflater); //初始化viewpager initView(); } /* * 底部導航欄切換後 由於沒有銷燬頂部設定導致如果沒有重新設定view * 導致底部切換後切回頂部頁面資料會消失等bug * 以下設定每次重新建立view即可 */ ViewGroup parent = (ViewGroup) view.getParent(); if (parent != null) { parent.removeView(view); } return view; } /*** * 初始化viewpager */ private void initView() { List<HTab> hTabs = HTabDb.getSelected(); for (int i = 0; i < hTabs.size(); i++) { OneFm1 fm1 = new OneFm1(); Bundle bundle = new Bundle(); bundle.putString("name", hTabs.get(i).getName()); fm1.setArguments(bundle); newsList.add(fm1); } //設定viewpager介面卡 adapter = new OneFmAdapter(getActivity().getSupportFragmentManager(),newsList); vp_.setAdapter(adapter); //兩個viewpager切換不重新載入 vp_.setOffscreenPageLimit(2); //設定預設 vp_.setCurrentItem(0); //設定viewpager監聽事件 vp_.setOnPageChangeListener(this); } /*** * 初始化頭部導航欄 * @param inflater */ private void initTab(LayoutInflater inflater) { List<HTab> hTabs = HTabDb.getSelected(); for (int i = 0; i < hTabs.size(); i++) { //設定頭部項佈局初始化資料 RadioButton rbButton = (RadioButton) inflater.inflate(R.layout.tab_rb, null); rbButton.setId(i); rbButton.setText(hTabs.get(i).getName()); LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); //加入RadioGroup rg_.addView(rbButton,params); } //預設點選 相關推薦Android實現頂部底部雙導航介面功能最近想弄一個雙導航功能,查看了許多資料,總算是實現了功能,這邊就算是給自己幾個筆記吧! 先來看看效果 那麼就開始實現了! 底部導航欄我選擇用FragmentTabHost+Fragment來實現,這個方法我覺得挺好用的,程式碼量也不多 首先是開始的activity_main.xml ?123456789101 android介面設計筆記(二)實現頂部底部二級導航欄下載demo:https://github.com/linliangliang/BottomNavagationBar 二級導航欄的實現是在之前學習導航欄的兩種實現方式的基礎上實現的。 1、https://blog.csdn.net/qq_25066049/article/details/8 android自定義底部Tab導航UI,專案整體介面框架android自定義底部Tab導航UI,專案整體介面框架 共享一個自己在開發過程中搭建的android專案介面框架,便於提高開發效率。 主要功能 1.使用Button自定義底部Tab和Title 2.點選底部Tab後使用Fragment切換頁面 3.主頁使用ViewPager滾動 Android ViewPager和Fragment實現仿微信導航介面及滑動效果1 先看看實現的效果: ps:上面每一幀Fragment中,包含是來自網路的圖片; 實現ViewPager+Fragment的頁面滑動和底部導航原理 主佈局檔案如下: <?xml version="1.0" encoding="utf-8"?> <L android實現異網雙卡雙待識別運營商網路檢測的程式碼 private void checkIMSi() { boolean simStateBySlotIdx = SimUtils.getSimStateBySlotIdx(this, 0); &n Android 實現視訊聊天1對1功能Android實現視訊聊天很難? 不不不,其實很簡單,使用聲網第三方sdk簡單上手 先看效果,我這裡用的是模擬器測試 大家可以看到兩個模擬器介面都會顯示對應內容。右上角是自己,左邊是對方。 專 android實現頂部弧形背景今天早上來了被IOS提醒安卓的背景是一個頂部為弧線的是否好實現。然後說他們是用兩個矩形拼湊的。於是我就想著實現。先去搜索發現了一個庫可以實現這樣的效果: 這是介紹的網址:http://www.codedata.cn/cdeta android實現炫酷水波紋介面利用正弦函式 1、這篇是兩條水波紋,樣子類似點融(理財),波浪上面還漂浮一個圖示,超炫酷 2、多條水波紋聯動效果(容易出現問題) 1)有種向右漂移的感覺 2)動著動著就重合了(大姨媽app) 利用2階貝塞爾曲線 1、android 繪製波浪線 2、 Android 貝 Android實現拍照相簿圖片上傳功能更改頭像功能不像修改資訊一樣直接提交引數就可以,需要上傳圖片檔案 我就直接貼程式碼了首先給出佈局檔案 <ImageView android:id="@+id/iv" android:layout_width="50d Android實現從底部彈出Dialog(和PopWindow實現的效果一樣)上菜,不,上圖: 相信上圖的效果,大家在android 裝置中經常碰到.有時候進行分享操作的時候-----要求從從底部自下而上彈出.上圖中的效果**既可以通過自定義Dialog實現也可以通過自定義PopWindow來實現.**關於popWindow android 實現城市選擇、聯絡人等功能的易拓展 RecyclerView 庫,包含自動索引,粘性等功能A RecyclerView with indexable, sticky and many other features. 輕鬆實現:選擇城市,選擇聯絡人等需要索引的功能 替代之前的 IndexableStickyListView(移至該分支),進行大幅度重構,效能優化,更易使用的 API,更易拓展的 Android 實現自動更新及強制更新功能引言 目的不言而喻,肯定是為了自己版本的迭代更新。想做到版本的完全控制手段還是比較多的。今天我要分享的這個方法是用蒲公英提供的版本查詢介面和版本下載介面來做的。有條件的同學也可以在自己的服務端開這兩個介面。 需求分析 我們要想實現這個自動更新的功能大致分三步 Android 實現記住使用者名稱和密碼的功能緒論 記住密碼功能要用到SharedPreferences類,這個類可以實現簡單的儲存,生成內部檔案,主要用於儲存視窗狀態,是不錯的一項知識,希望大家一起學習。 結果圖如下: 程式碼 Welcome.java package com.gxs.login; import co android-----------實現微信登入和分享功能。1.通過微信官網獲得APPID和.jks檔案。 (.jks檔案的獲得是通過AS中的Build-Generate Signed APK-如果已有.jks就選擇已有的路徑,對應好兩次的密碼。 ) 2.導依賴 compile 'com.tencent.mm.opensdk:wec Android實現從底部彈出的Dialog<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xml android實現頂部滑動列表(1)定義一個xml viewpager控制元件<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/an 【隨記】JQuery實現表格的雙擊編輯功能(自動適應表格高度和寬度)<html> <head> <!-- 設定JQuery的路徑 --> <script src="../jQuery/jquery-1.11.2.min.js"></script> <script type=" Android實現頂部輪播圖1,首先,宣告一些引用: SharedPreferences sp; private ViewPager vp_top_pic; private List viewList; private ViewPager rollViewPager; private LinearL Android實現朋友圈全文/收起的功能看下效果圖: 首先我們先理解收起和全文的邏輯: 假如產品設定:文字超過三行就要摺疊 1 如果文字不超過三行,那麼就不顯示“全文”和“收起” 2 如果超過了三行顯示“全文”,點選全文就可以看到所有文字,同時最下面顯示“收起” Android開發丶底部導航欄的主介面實現在主流app中,應用的主介面都是底部含有多個標籤的導航欄,點選可以切換到相應的介面,如圖: 接下來將描述下其實現過程。 1.首先是分析介面,底部導航欄我們可以用一個佔滿螢幕寬度、包裹著數個標籤TextView、方向為橫向horizontal的線性佈局LinearLay |