2014-02-19 70 views

回答

0

使用Action Sherlock(小於api 3)。欲瞭解更多請點擊here。謝謝:)

+0

OP在哪裏提到他在api上<3? –

+0

我正在使用appcompat庫。 – Sunny

0

MainActivity

public class MainActivity extends SherlockActivity { 

private final Handler handler = new Handler(); 

private boolean useLogo = false; 
private boolean showHomeUp = true; 

ActionMode mMode; 

Handler mHandler = new Handler(); 
Runnable mProgressRunner = new Runnable() { 
    @Override 
    public void run() { 
     mProgress += 2; 

     //Normalize our progress along the progress bar's scale 
     int progress = (Window.PROGRESS_END - Window.PROGRESS_START)/100 * mProgress; 
     setSupportProgress(progress); 

     if (mProgress < 100) { 
      mHandler.postDelayed(mProgressRunner, 50); 
     } 
    } 
}; 

private int mProgress = 100; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    requestWindowFeature(Window.FEATURE_PROGRESS); 

    setContentView(R.layout.main); 
    final ActionBar ab = getSupportActionBar(); 

    // set defaults for logo & home up 
    ab.setDisplayHomeAsUpEnabled(showHomeUp); 
    ab.setDisplayUseLogoEnabled(useLogo); 


    ((Button)findViewById(R.id.progress)).setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (mProgress == 100) { 
       mProgress = 0; 
       mProgressRunner.run(); 
      } 
     } 
    }); 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getSupportMenuInflater().inflate(R.menu.main_menu, menu); 

    // set up a listener for the refresh item 
    final MenuItem refresh = (MenuItem) menu.findItem(R.id.menu_refresh); 
    refresh.setOnMenuItemClickListener(new OnMenuItemClickListener() { 
     // on selecting show progress spinner for 1s 
     public boolean onMenuItemClick(MenuItem item) { 
      // item.setActionView(R.layout.progress_action); 
      handler.postDelayed(new Runnable() { 
       public void run() { 
        refresh.setActionView(null); 
       } 
      }, 1000); 
      return false; 
     } 
    }); 
    return super.onCreateOptionsMenu(menu); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case android.R.id.home: 
     // TODO handle clicking the app icon/logo 
     return false; 
    case R.id.menu_refresh: 
     // switch to a progress animation 
     item.setActionView(R.layout.indeterminate_progress_action); 
     return true; 

    default: 
     return super.onOptionsItemSelected(item); 
    } 
}} 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/root" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="horizontal" > 
<Button 
    android:id="@+id/progress" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Progress" /> 
</LinearLayout> 

indeterminate_progress_action.xml

<?xml version="1.0" encoding="utf-8"?> 
<ProgressBar 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:indeterminate="true" 
android:layout_marginLeft="@dimen/progress_action_padding" 
android:layout_marginRight="@dimen/progress_action_padding" /> 

菜單/ main_menu.xml

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:id="@+id/menu_refresh" android:showAsAction="ifRoom" 
    android:title="@string/action_label_refresh" 
    android:icon="@drawable/ic_menu_refresh_holo_dark" /> 
</menu> 

AndroidManifest.xml中

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.actionbarstyleexample" 
android:versionCode="1" 
android:versionName="1.0"> 

<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="19" /> 

<application android:label="@string/app_name" 
    android:icon="@drawable/ic_launcher" 
    android:theme="@style/AppTheme"> 

    <activity android:name="com.actionbarsherlock.sample.styled.MainActivity" 
     android:label="@string/activity_name" 
     android:logo="@drawable/ad_logo" 
     android:uiOptions="splitActionBarWhenNarrow"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN"/> 
      <category android:name="android.intent.category.DEFAULT"/> 
      <category android:name="android.intent.category.LAUNCHER"/> 
     </intent-filter> 
    </activity> 

</application>