2011-08-17 34 views
3
视图

我想要的水平进度栏添加到我的观点,像这样:访问里面的ActionBar

RES/menu.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> 

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

回答

1

onCreate()之后,您可以通过findViewById()像普通一样简单地访问它。问题是由别的东西引起的。

+0

你介意记录这种行为的根本原因是什么? – Necronet

+0

似乎不工作,一直返回null – max4ever

+0

@max4ever确保它已创建。可能需要使用'postDelayed'来等待渲染AB项目的视图。 –

-1

以下是一个非常有用的actionBar.Hope链接,你将能够实现你想要的。

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() 

    } 

} 
+0

它并不抱歉,我试图找出如何在v11 ActionBar组件中找到视图,而不是“自己构建”Action Bar。 – Graeme

+0

我更新了我的答案。 – jainal

+0

再次,我想知道如何访问视图附加到android.app.ActionBar不在自定义ActionBar中的视图。 – Graeme

2

您可以覆盖onCreateOptionsMenu有赶上你的观点:

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

或通过搜索ID

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

我插入自定义操作栏中的下拉菜单,能够通过这种方式获得对其的控制。

5

可以获取操作项目的视图。但是,请注意,有时候操作项会进入到溢出菜单中,因此您可能会得到空值。

那么,你该怎么做呢?

这里是一个示例代码:

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); 
     ... 

这是使用actionBarSherlock库时,在Android 4.1.2和Android 2.3.5测试。

另一种选择是使用更广泛的方式,在showcaseView libraryhere上使用。

+1

这解决了我的问题。需要使用处理程序来确保菜单项已添加到视图层次结构中(通过在onCreateOptionsMenu()完成之后入队) – d370urn3ur