2015-12-23 20 views
2

我無法獲取工具欄內的Framelayout或Textview的Id。請看看:Android:獲取android.support.v7.widget.Toolbar內創建的任何視圖的ID時出錯

<android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="@color/colorAppBg" 
      app:popupTheme="@style/AppTheme.PopupOverlay" > 
      <FrameLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 
      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:src="@drawable/logo"/> 

       <FrameLayout 
        android:id="@+id/fl_message_bg" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:visibility="gone" 
        android:layout_gravity="right|center"> 
        <ImageView 
         android:id="@+id/iv_cart" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="bottom" 
         android:layout_marginRight="12dp" 
         android:layout_marginTop="2dp" 
         android:src="@drawable/envelope"/> 
        <TextView 
         android:id="@+id/tv_message_count" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="top|right" 
         android:text="10" 
         android:textColor="@color/colorWhite" 
         android:gravity="center" 
         android:textSize="12sp" 
         android:visibility="visible" 
         android:background="@drawable/circle_notification"/> 
       </FrameLayout>    
      </FrameLayout> 
     </android.support.v7.widget.Toolbar> 
    </android.support.design.widget.AppBarLayout> 

這是我無論是在延伸AppCompatActivity片段或活動找到那些視圖的ID碼。 內部片段:

`private Activity activity; 
@Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     this.activity =(Activity)context; 
    } 
flMessageCountBg = (FrameLayout) activity.findViewById(R.id.fl_message_bg);` 

在活動:

的FrameLayout flMessageCountBg =(的FrameLayout)findViewById(R.id.fl_message_bg);

我在兩種情況下都是空的。請幫助我解決這個問題。提前致謝。

回答

2

您使用一個支持庫,所以我應該設置工具欄爲支援行動吧。然後,你可以通過使用toolbar.findviewbyid(ID)

Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar); 
setSupportActionBar(toolbar); 
ImageView iv = (ImageView) toolbar.findViewById(R.id.iv_cart); 

,如果您使用的是片段讓工具欄裏面的觀點和你的活動延伸AppCompatActivity,那麼你可以設置支持操作欄如下:

((AppCompatActivity) getActivity()).setSupportActionBar(toolbar); 
+0

請給你的代碼添加一些解釋! –

+1

@ρss解釋它 –

+0

@Malek謝謝。得到這個'嘗試調用虛擬方法'android.view.View android.support.v7.widget.Toolbar.findViewById(int)'在一個空對象引用上' – Harry

1

嘗試這種方式

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
FrameLayout flMessageCountBg = (FrameLayout) toolbar.findViewById(R.id.fl_message_bg); 
+0

謝謝。我嘗試了你的溶劑,但我仍然** ** null ** FrameLayout ** flMessageCountBg **。我的Activity擴展了AppCompatActivity。 – Harry

+0

更新您的新代碼 –

+0

和地點活動 –

1

活動讓您工具欄片段那麼你得到你的FrameLayout工具欄這樣的:

Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar); 
FrameLayout flMessageCountBg = (FrameLayout) toolbar.findViewById(R.id.fl_message_bg); 
+0

謝謝。我已經嘗試過,但我仍然** ** null ** Framelayout ** flMessageCountBg **。 – Harry

+0

謝謝你使用這個'flMessageCountBg =(FrameLayout)getActivity()。fragment完成它。findViewById(R.id.fl_message_bg);' – Harry

0

的解決方案是非常簡單的獲取ID(內部片段)的內android.support.v7.widget.Toolbar創建的視圖的。 flMessageCountBg = (FrameLayout) getActivity().findViewById(R.id.fl_message_bg);

我已經創建了三個佈局文件夾佈局,佈局大口和佈局XLARGE端口想念更改ID,而我測試的7寸平板,其挑選編號從佈局large-港口。

@馬利克,@語境和@bhargav

感謝您的幫助。

相關問題