2015-07-20 41 views
0

toolbar.xml獲取片段的Android工具欄子視圖

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="start" 
    android:layout_gravity="start" 
    android:background="@color/primary" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
    android:elevation="4dp"> 
    <!-- Color is Brown 500 --> 
    <ProgressBar 
     android:id="@+id/toolbar_progress_bar" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:visibility="gone" 
     android:indeterminateTint="#795548" 
     android:indeterminateTintMode="src_in" 
     android:layout_gravity="right" 
     /> 
</android.support.v7.widget.Toolbar> 
    <!--android:paddingTop="2dp"/--> 

activity_main.xml中

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:custom="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v4.widget.DrawerLayout 
     android:id="@+id/drawerLayout" 
     android:elevation="7sp" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <RelativeLayout 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <include 
       android:id="@+id/toolbar" 
       layout="@layout/toolbar_main" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 
      </include> 

      <FrameLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:id="@+id/container" 
       android:layout_below="@id/toolbar"/> 

     </RelativeLayout> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/recyclerView" 
      android:layout_width="320dp" 
      android:background="@android:color/white" 
      android:layout_height="match_parent" 
      android:layout_gravity="start" 
      android:scrollbars="vertical"/> 

    </android.support.v4.widget.DrawerLayout> 

</RelativeLayout> 

我怎樣才能訪問進度的工具欄,並將其設置爲可見在我片段代碼?

我想:

AppCompatActivity act = (AppCompatActivity) getActivity(); 
    if (act.getSupportActionBar() != null) { 
     toolbar = (android.support.v7.widget.Toolbar) act.getSupportActionBar().getCustomView(); 
     progress = (ProgressBar)toolbar.findViewById(R.id.toolbar_progress_bar); 
    } 

但要

java.lang.ClassCastException:android.widget.RelativeLayout不能 投地android.support.v7.widget.Toolbar

回答

2

您是否嘗試過

toolbar.findViewById(R.id.toolbar_progress_bar).setVisibility(View.VISIBLE); 

對我來說它的工作原理!我在工具欄中有一個ImageView