2

我需要在进度指示器上ActionBar占用空间,即使它不可见。

我在用:

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

接着:

setProgressBarIndeterminateVisibility(false);

这似乎将进度条的可见性设置为GONEand not INVISIBLE,这使得我在操作栏上的动态选项卡数量可以暂时移动和删除我的应用程序标题,直到进度完成。

考虑在操作栏上添加一个空操作,我可以在指示进度之前将其删除,并在完成后再次添加。还有另一种不那么混乱的方法吗?

4

1 回答 1

0

我可以对自己的问题提出最佳答案(一种解决方法)。

首先,我需要按照这个例子知道我ActionBar的是否分裂。这是决定是否使用默认进度条。拆分时,我的自定义进度条最终会出现在 的底部,这是不希望的。ActionBarActionBarActionBar

/values/bools.xml:

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

/值端口/bools.xml:

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

/值-大/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);
}

自定义进度指示器的布局:

/layout/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);
        }
    }
}
于 2013-08-30T14:20:11.270 回答