2013-08-29 37 views
2

我需要ActionBar上的進度指示器佔用空間,即使它不可見。如何讓動作欄上的進度條佔用空間,即使不可見?

我使用:

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 
setProgressBarIndeterminateVisibility(true); 

然後:

setProgressBarIndeterminateVisibility(false); 

這似乎進度條可見性設置爲GONE,而不是INVISIBLE,這使得操作欄上我的標籤動態量暫時移動並刪除我的應用標題,直到完成進度。

考慮在動作條上添加一個空動作,在動作條上可以刪除前指示進度並在完成時再次添加。有沒有另一種混亂的方式來做到這一點?

回答

0

最好的答案我可以想出我自己的問題(一種解決方法)。

首先我需要知道我的ActionBar是否被拆分,例如this的例子。這是爲了確定當ActionBar是否使用默認進度條。當ActionBar被分割時,我的自定義進度條將會在ActionBar的底部結束,這是不期望的。

/values/bools.xml:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <bool name="split_action_bar">false</bool> 
</resources> 

/values-port/bools.xml:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <bool name="split_action_bar">true</bool> 
</resources> 

/values-large/bools.xml:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <bool name="split_action_bar">false</bool> 
</resources> 

在我的活動,獲取與價值:

private boolean isActionBarSplitted() { 
    return getResources().getBoolean(R.bool.split_action_bar); 
} 

我有我的選項菜單中設置了我的自定義進度指示器MenuItem當我的應用程序是大型設備或在狹窄的設備橫向模式使用。

/menu/options_menu.xml:

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item 
     android:id="@+id/custom_progress_indicator" 
     android:showAsAction="always"/> 
    <item 
     android:id="@+id/another_menu_item" 
     android:icon="@android:drawable/btn_default" 
     android:showAsAction="ifRoom"/> 
    <item 
     android:id="@+id/yet_another_menu_item" 
     android:icon="@android:drawable/btn_default" 
     android:showAsAction="ifRoom"/> 
</menu> 

在我Activity我取MenuItem和禁用它,所以它纔會真正是無形的,但不是GONE

@Override 
public boolean onPrepareOptionsMenu(Menu menu) { 
    mCustomProgressIndicator = menu.findItem(R.id.custom_progress_indicator); 
    mCustomProgressIndicator.setEnabled(false); 
    return super.onPrepareOptionsMenu(menu); 
} 

佈局的自定義進度指示器:

/佈局/ custom_progress_bar。XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ProgressBar android:id="@+id/progress" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
</LinearLayout> 

我有開始的兩個功能,並停止顯示進度:

/** 
* Indicates progress on the action bar 
*/ 
private void startIndicatingProgress() { 
    mIndicatingProgress = true; 
    if (isActionBarSplitted()) { 
     // if we have a split action bar, use the default progress indicator 
     setProgressBarIndeterminateVisibility(true); 
    } else { 
     if (mCustomProgressIndicator != null) { 
      // this function may get called during onResume where 
      // mCustomProgressIndicator is not set 

      LayoutInflater inflater = (LayoutInflater) 
        getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View progressBarLayoutView = inflater.inflate(
        R.layout.custom_progress_bar, null); 
      View progressBar = progressBarLayoutView 
        .findViewById(R.id.progress); 
      LayoutParams layoutParams = progressBar.getLayoutParams(); 

      // well now, not sure what the width of MenuItems is (since it 
      // seems impossible to find out) 
      // so adjusting the height of the ActionBar with 8, better than 
      // to hardcode a value I guess 
      layoutParams.width = actionBar.getHeight() + 8; 
      mCustomProgressIndicator.setActionView(progressBarLayoutView); 
     } 
    } 
} 

/** 
* Returns true if the ActionBar is currently split 
* 
* @return 
*/ 
private boolean isActionBarSplitted() { 
    return getResources().getBoolean(R.bool.split_action_bar); 
} 

/** 
* Stops indicating progress 
*/ 
private void stopIndicatingProgress() { 
    mIndicatingProgress = false; 
    // always stop default progress indicator 
    setProgressBarIndeterminateVisibility(false); 
    // this function may get called in onResume where 
    // mCustomProgressIndicator is not set 
    if (mCustomProgressIndicator != null) { 
     mCustomProgressIndicator.setActionView(null); 
    } 
} 

你可能會認爲這將是確定的,但沒有,改變方向我自當ProgressBar變收縮,所以我需要檢測方向更改並重置自定義進度欄。這對我來說是個好主意,因爲在小設備上我需要在默認進度條和自定義之間切換,因爲我的活動是android:uiOptions="splitActionBarWhenNarrow"

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE 
      || newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { 
     if (mIndicatingProgress) { 
      // restart indicating progress to avoid feature of shrinking 
      // custom progress 
      // bar and switch between custom and default if needed 
      stopIndicatingProgress(); 
      startIndicatingProgress(); 
     } 

     // also remove custom progress indicator if split action bar 
     // so it won't take up space on the action bar when using 
     // default progress indicator 
     if (isActionBarSplitted()) { 
      // this seems to be the equivalent of View.setVisibility(View.GONE) 
      mCustomProgressIndicator.setVisible(false); 
     } else { 
      mCustomProgressIndicator.setVisible(true); 
     } 
    } 
} 
相關問題