Android数据绑定SetSupportActionBar

问题描述 投票:8回答:3

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

谢谢

android xml android-toolbar
3个回答
15
投票

您可以访问工具栏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/>,您仍然可以访问它


1
投票

在Android Studio中创建项目,然后您可以使用下面的代码访问工具栏:

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

0
投票
<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>
© www.soinside.com 2019 - 2024. All rights reserved.