android底部actionbar
㈠ Android知識串講(1) 底部導航欄遮擋|轉屏鎖定|ActionBar隱藏
方法一:隱藏
Android中布局內容被底部系統導航欄遮擋
  方法二:內容上移 
    Android手機底部NavigationBar擋住界面的解決方法 
在 Manifest.xml 文件中設腔備置 Activity 的屬性
  Android布局界面隱藏頂部導航欄 
    Android隱藏和顯示虛擬導航欄 
  Android 獲取手機存儲總大小,系統佔用空間 
    Android 獲取稿圓備屏幕寬度和高度的幾種鍵毀方法 
    Android 獲取電池相關信息 
    Android電量計重要的類及函數介紹 
    安卓5.0後獲取所有運行的進程信息 
    Android獲取內存(RAM)大小信息 
    android 幾種殺進程的方式 
    Android開發中 獲取App緩存大小以及清除緩存 
Looper.getMainLooper()使用誤區
Android中通過資源文件獲取drawable的幾種方法
Fragment向Activity傳遞值
注意:
㈡ Android怎麼動態更改actionbar的背景顏色
Android動態更改actionbar的背景顏步驟:
在backgroud下面再寫一個style,修改values-v14文件夾下的style.xml文件
[html] view plain
<resources xmlns:android="http://schemas.android.com/apk/res/android">  
  
    <!--  
        Base application theme for API 14+. This theme completely replaces  
        AppBaseTheme from BOTH res/values/styles.xml and  
        res/values-v11/styles.xml on API 14+ devices.  
    -->  
    <style name="AppBaseTheme" parent="android:Theme.Holo.Light">  
        <!-- API 14 theme customizations can go here. -->  
        <item name="android:actionBarStyle">@style/my_actionbar_style</item>   
    </style>  
      
    <style name="my_actionbar_style" parent="@android:style/Widget.Holo.Light.ActionBar">  
        <item name="android:background">#647b97</item>  
        <item name="android:titleTextStyle">@style/AcBar_titleStyle</item>  
[html] view plain
<item name="android:backgroundStacked">#4A708B</item>   <!-- 分離成tab時的tab顏色 -->  
[html] view plain
   <item name="android:backgroundSplit">@color/ab_backgroundSplit_color</item>  
    </style>  
    <style name="AcBar_titleStyle">    
          
        <item name="android:textColor">#FFFFFF</item>    
    </style>  
   
</resources>  
   <item name="android:backgroundSplit">@color/ab_backgroundSplit_color</item> actionbar item
這樣就分離在底部時候的顏色。
㈢ android如何去掉actionbar
當使用Android中的ActionBar控制項時,如果想要隱藏上面的ActionBar,可以使用如下的代碼:
getSupportActionBar().hide();//隱藏掉整個ActionBar,包括下面的Tabs
上面的代碼會將整個ActionBar都隱藏掉,包括ActionBar中的Tab分頁標簽,如果想要保留分頁標簽的話,可以使用如下的代碼:
ActionBar actionBar = getSupportActionBar();//高版本可以換成 ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
//會保留tab標簽
另外還有一種更簡單的方式來移除ActionBar,在setContent之前調用下面這句,保證沒有ActionBar
requestWindowFeature(Window.FEATURE_NO_TITLE);
㈣ android怎麼設置狀態欄和actionbar同顏色
android 布局 layout relativelayout
除了沉浸模式外,Android 4.4還有新的API,能使應用內的狀態欄和虛擬按鈕透明。其他更多的Android 4.4 APIs可以看這里。
如果要使應用內的狀態欄和虛擬按鈕變成透明有兩種方法。
一種是代碼方式:
?1
2
3Window window = getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, 
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, 
WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
另外一種是使用兩個新的主題風格:
Theme.Holo.NoActionBar.TranslucentDecor和Theme.Holo.Light.NoActionBar.TranslucentDecor。
但是這種方式只支持Android4.4以上系統,所以為了保持兼容性,我們還是採用代碼方式比較好。只需要先判斷,如果是4.4以上系統才啟用代碼。
開啟後上下系統欄都透明了。
但是如果應用本身是帶有actionbar或者標題欄的話會就會變得比較尷尬,內容會在上面露出來。這個時候需要在布局文件里加入android:fitsSystemWindows="true"。
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#c8c8c8" >
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
但是這樣的話內容就不能從透明的虛擬按鈕下穿過,沒原來那麼好看。我們可以按照以前一樣把根布局設置一個高度為系統欄高度和ActionBar高度的內邊距就可以。
同時關於獲取ActionBar和狀態欄的高度,發現用原來的方法有時候會獲取的值為0。自己google找了一下,找到兩個前輩提供的獲取高度方法,獲取ActionBar高度,獲取狀態欄高度。
8if (android.os.Build.VERSION.SDK_INT > 18) {
Window window = getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, 
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, 
WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//設置根布局的內邊距
RelativeLayout relativeLayout = (RelativeLayout) 
findViewById(R.id.layout);
relativeLayout.setPadding(0, getActionBarHeight()+getStatusBarHeight(), 0, 
0);
}
27// 獲取手機狀態欄高度
public int getStatusBarHeight() {
Class c = null;
Object obj = null;
Field field = null;
int x = 0, statusBarHeight = 0;
try {
c = Class.forName("com.android.internal.R$dimen");
obj = c.newInstance();
field = c.getField("status_bar_height");
x = Integer.parseInt(field.get(obj).toString());
statusBarHeight = getResources().getDimensionPixelSize(x);
} catch (Exception e1) {
e1.printStackTrace();
}
return statusBarHeight;
}
// 獲取ActionBar的高度
public int getActionBarHeight() {
TypedValue tv = new TypedValue();
int actionBarHeight = 0;
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))// 
如果資源是存在的、有效的
{
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, 
getResources().getDisplayMetrics());
}
return actionBarHeight;
}
接下來,因為我自己寫的一些應用是暗色的主題的,會導致透明的狀態欄和ActionBar顏色不太協調。看到有一些應用是把狀態欄的顏色設置成和ActionBar一樣,這種解決方法也不錯。
具體是怎麼實現的也不太清楚,我自己猜測寫了一個差不多狀態欄。我是直接在根視圖加入一個高度為狀態欄高度的TextView,背景設置為和ActionBar一樣。具體代碼如下:
8// 創建TextView
TextView textView = new TextView(this);
LinearLayout.LayoutParams lParams = new 
LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, getStatusBarHeight());
textView.setBackgroundColor(Color.parseColor("#3F9FE0"));
textView.setLayoutParams(lParams);
// 獲得根視圖並把TextView加進去。
ViewGroup view = (ViewGroup) getWindow().getDecorView();
view.addView(textView);
在模擬器上看還行,但是在實際的手機當中總感覺ActionBar有點過大,所以我在背景色里加入了一些漸變,在實體手機中就比較好看一點,不會覺得ActionBar太寬了。
android:startColor="#c8c8c8"
android:endColor="#3F9FE0"
android:angle="270"
android:type="linear"/>
㈤ android 怎樣獲取當前自己創建的actionbar
作為Android 3.0之後引入的新的對象,ActionBar可以說是一個方便快捷的導航神器。它可以作為活動的標題,突出活動的一些關鍵操作(如「搜索」、「創建」、「共享」等)、作為菜單的靈活使用,還可以實現類似TabWidget的標簽功能以及下拉導航的功能,系統能夠很好根據不同的屏幕配置來適應ActionBar的外觀,配合起Fragemtn可謂是十分強大。
          那麼,對於今天的主角ActionBar怎麼去添加?在Android3.0默認主題HloleFraphic(全息)主題中,已經創造了ActionBar,所以只要targetSdkVersion的值不低於11,創建的Activity中默認都會帶有ActionBar。例如:
[html] view plain
<manifest ... >    
    <uses-sdk android:minSdkVersion="4"  
              android:targetSdkVersion="11" />    
    ...    
</manifest>  
          當然了,如果你不想為一個特定的Activity設置Action Bar,設置Activity主題為Theme.Holo.NoActionBar。
[html] view plain
<activity android:theme="@android:style/Theme.Holo.NoActionBar">  
或者在運行時通過調用hide()隱藏Action Bar。自然也有show()。
[html] view plain
ActionBar actionBar = getActionBar();    
actionBar.hide();  
          下面我們從下拉導航、視窗操作、標簽導航三個方面逐一討論ActionBar
         第一,下拉導航
                    下拉導航最典型的應用場景就是在Google+中的使用,效果如下圖:
圖1;Google+                             圖2:本文示例
實現此效果分如下幾個步驟:
          1.初始化一個SpinnerAdapter 
[java] view plain
SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this,  
                R.array.action_list,  
                android.R.layout.simple_spinner_dropdown_item);  
  2.生成一個OnNavigationListener來響應ActionBar的菜單項點擊操作
[java] view plain
/** 
     * 在這里配合Fragment,實現不同的頁面導航 
     */  
    OnNavigationListener mOnNavigationListener = new OnNavigationListener() {  
  
        @Override  
        public boolean onNavigationItemSelected(int position, long itemId) {  
            Fragment newFragment = null;  
            switch (position) {  
            case 0:  
                newFragment = new Fragment1();  
                break;  
            case 1:  
                newFragment = new Fragment2();  
                break;  
            case 2:  
                newFragment = new Fragment3();  
                break;  
            default:  
                break;  
            }  
            getSupportFragmentManager().beginTransaction()  
                    .replace(R.id.container, newFragment, strings[position])  
                    .commit();  
            return true;  
        }  
    };  
3,將生成好的適配去和監聽器塞給ActionBar
[java] view plain
actionBar = getActionBar();  
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);//導航模式必須設為NAVIGATION_MODE_LIST  
actionBar.setListNavigationCallbacks(mSpinnerAdapter,  
        mOnNavigationListener);
