繁体   English   中英

Android 数据绑定 SetSupportActionBar

[英]Android Data Binding SetSupportActionBar

嗨,我想知道这个函数是否有使用 Android 数据绑定库的 xml 标记,或者如何在没有 findViewById() 方法的情况下实现这一点

谢谢

您可以使用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/>您仍然可以访问它

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

ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
setSupportActionBar(binding.toolbar);
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    >
  <data>
    <variable
        type="com.example.viewModel"
        name="viewModel"
        />
  </data>
  <android.support.v7.widget.Toolbar
      android:layout_width="match_parent"
      android:layout_height="?actionBarSize"
      app:title="@{viewModel.title}"/>
</layout>

你可以这样使用它。 经过多次搜索,这为我解答。

  1. id用于包含,例如我的工具栏 id 是tb ,包含idmtoolbar

  2. 现在使用setSupportActionBar(mainBinding.mtoolbar.tb);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM