2011-08-17 35 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