3

我正在尝试向我的视图添加一个水平进度条,如下所示:

资源/菜单.xml

<item   android:id="@+id/menuItemProgress"
        android:title="Progress"
        android:actionLayout="@layout/component_cancellable_progressbar"
        android:showAsAction="always"/>

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>

如何从操作中访问此 ProgressBar(使其可见/不可见/进度)?

4

4 回答 4

5

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 .

于 2013-08-25T09:20:35.777 回答
2

onCreate() 之后,您可以像往常一样通过 findViewById() 简单地访问它。问题是由其他原因引起的。

于 2011-08-17T10:42:19.660 回答
2

您可以覆盖 onCreateOptionsMenu 并在那里捕捉您的视图:

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

或通过搜索 id

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

我在操作栏中插入了一个自定义下拉菜单,并且能够通过这种方式对其进行控制。

于 2013-07-22T21:52:24.740 回答
-1

以下是 actionBar 的一个非常有用的链接。希望您能够实现您想要的。

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() { ... }


}

然后从您的活动中控制您的进度条,如下所示。

public class MainActivity extends Activity {

    private ActionBar mActionBar;



    @Override

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

        mActionBar.showProgressbar()

     }

}
于 2011-08-17T09:12:10.313 回答