2016-01-06 70 views
3

您好我不知道是否有使用Android的数据绑定库或如何实现这一点没有findViewById()方法,此功能的XML标签Android的数据绑定SetSupportActionBar

谢谢

+0

http://developer.android.com/reference/android/view/View.html 有以下 –

回答

7

您可以访问的实例工具栏using views by id功能

<android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|enterAlways" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

然后在你的onCreate()方法请执行下列操作

ActivityGalleryBinding binding = DataBindingUtil 
    .setContentView(this, R.layout.activity_gallery); 
binding.setViewModel(new GalleryModel(this)); 
//set it like this 
setSupportActionBar(binding.<location>.<of>.<your>.toolbar); 

如果你的工具栏是其它XML组件(具有<include/>参考)内,你仍然可以只要你提供和@id访问到<include/>

+0

叶氏链接等方法,想通了这一点有关2个月前,欢呼 –

+0

如果工具栏包含在包含布局中,该怎么办?或者如果在包含的布局中有多个视图? – ARiF

+0

+10为'include',谢谢 – m02ph3u5

1

项目Android Studio中,那么你可以在下面使用访问工具栏代码:

ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); 
setSupportActionBar(binding.toolbar);