簡體   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