开发者

Accessing a view inside ActionBar

I'm trying to add an Horizontal Progress bar to my view like so:

res/menu.xml

<item   android:id="@+id/menuItemProgress"
        android:title="Progress"
        android:actionLayout="@layout/component_cancellable_progressbar"
        android:showAsAction="always"/>开发者_如何学JAVA;

component_cancellable_progressbar.xml

<FrameLayout    xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/searchProgressWrapper"
                android:layout_height="wrap_content" 
                android:layout_width="wrap_content">
<ProgressBar    android:id="@+id/searchProgress"
                android:layout_height="30dp"
                android:layout_width="150dp"
                style="@style/Custom.Widget.ProgressBar.Horizontal"
                android:max="100" />
<ImageView      android:id="@+id/cancelSearch"
                android:layout_height="wrap_content" 
                android:layout_width="wrap_content"
                android:paddingRight="8dp" 
                android:layout_gravity="right|center_vertical" 
                android:src="@android:drawable/ic_notification_clear_all"
                android:scaleType="fitXY" />
</FrameLayout>

How do I access this ProgressBar from within an Action (To make it Visisble / Invisible / Progress) ?


it is possible to get the view of the action item.

however, do note that sometimes action items get to be inside the overflow menu so you might get a null instead.

so, how can you do it?

here's a sample code:

public boolean onCreateOptionsMenu(final Menu menu) {
  getSupportMenuInflater().inflate(R.menu.main, menu);
  new Handler().post(new Runnable() {
    @Override
    public void run() {
      final View syncItemView = findViewById(R.id.action_search);
      ...

this was tested when using actionBarSherlock library, on android 4.1.2 and android 2.3.5 .

another alternative is to use a more extensive way , used on the showcaseView library, here .


onCreate() and after you can simply access it via findViewById() like normal. Problem was caused by something else.


You can override onCreateOptionsMenu and catch your view there:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
  menu.getItem(0).getActionView();
  ...

or by searching the id

View v = (View) menu.findItem(R.id.search).getActionView();

I inserted a custom drop down menu in the action bar and was able to gain control of it this way.


Following is a very helpful link for actionBar.Hope you will able to implement what you want.

http://thiranjith.wordpress.com/2011/07/15/actionbar-design-pattern-example-for-android/

public class ActionBar extends FrameLayout {

      private ProgressBar mProgress;



      public ActionBar(Context context, AttributeSet attrs) {

        super(context, attrs);

        mInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       FrameLayout barView = (RelativeLayout) mInflater.inflate(R.layout.actionbar, null);

        addView(barView);

       mProgress = (ProgressBar) barView.findViewById(R.id.actionbar_progress);

    public void showProgressBar() { ... }

    public void hideProgressBar() { ... }

    public boolean isProgressBarVisible() { ... }


}

Then from your activity control your progressbar like following.

public class MainActivity extends Activity {

    private ActionBar mActionBar;



    @Override

    public void onCreate(Bundle savedInstanceState) {
        mActionBar = (ActionBar) findViewById(R.id.actionBar);

        mActionBar.showProgressbar()

     }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜