猿问

使用 ViewBinding 进行多种布局

我正在尝试将findViewById应用程序中的调用切换到新添加的调用ViewBinding,但与调用的视图以外的视图交互时遇到问题setContentView()。


我膨胀AppBarMainBinding 的方式是否错误?或者我还缺少其他东西吗?


public class MainActivity extends AppCompatActivity

        implements NavigationView.OnNavigationItemSelectedListener {

...

    private ActivityMainBinding mActivityMainBinding;

    private AppBarMainBinding mAppBarMainBinding;

...

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        mActivityMainBinding = ActivityMainBinding.inflate(getLayoutInflater());

        setContentView(mActivityMainBinding.getRoot());


        mAppBarMainBinding = AppBarMainBinding.inflate(getLayoutInflater());

        setSupportActionBar(mAppBarMainBinding.toolbar);


        mAppBarMainBinding.fabCollapse.fab_collapse.setOnClickListener(view -> {

            Log.d(TAG, "Clicked fab_collapse");

        });

   }

}

包含activity_main.xml和<include layout="@layout/app_bar_main" />菜单


app_bar_main.xml包含一个工具栏和一些操作按钮


在这种情况下,如果我setContentView(mActivityMainBinding.getRoot())在主活动的 onCreat() 中调用 ,应用程序将启动activity_main布局并显示操作按钮和工具栏占位符,但所有按钮都不起作用。单击操作按钮不会调用它,setOnClickListener并且工具栏为空白(它没有实现setSupportActionBar(toolbar))


活动主文件


<?xml version="1.0" encoding="utf-8"?>

<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/drawer_layout"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:fitsSystemWindows="true"

    tools:openDrawer="start">

</androidx.drawerlayout.widget.DrawerLayout>


千巷猫影
浏览 198回答 4
4回答

白衣染霜花

使用视图绑定时,将为每个 xml 生成一个 Java 绑定文件。问题是当您修改 xml 时,它不会重新生成 Java 文件。因此,当我向 Activity_main.xml 中的 App_bar_main 添加 ID 标签时,我收到一条错误消息,指出它找不到该标签,不是因为它不喜欢它,而是因为它仍在使用旧的 Java 文件。使缓存失效并重新启动也不会影响那些生成的 java 绑定文件。重新生成文件的唯一方法是删除它们。添加 ID 后,我只需使用其 ID 调用 appBarMain,而不是尝试膨胀其布局。public class MainActivity extends AppCompatActivity&nbsp; &nbsp; &nbsp; &nbsp; implements NavigationView.OnNavigationItemSelectedListener {...&nbsp; &nbsp; private ActivityMainBinding mActivityMainBinding;&nbsp; &nbsp; private AppBarMainBinding mAppBarMainBinding;...&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; mActivityMainBinding = ActivityMainBinding.inflate(getLayoutInflater());&nbsp; &nbsp; &nbsp; &nbsp; setContentView(mActivityMainBinding.getRoot());&nbsp; &nbsp; &nbsp; &nbsp; mAppBarMainBinding = mActivityMainBinding.appBarMain;&nbsp; &nbsp; &nbsp; &nbsp; setSupportActionBar(mAppBarMainBinding.toolbar);&nbsp; &nbsp; &nbsp; &nbsp; mAppBarMainBinding.fabCollapse.fab_collapse.setOnClickListener(view -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, "Clicked fab_collapse");&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp;}}活动主文件<?xml version="1.0" encoding="utf-8"?><androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; xmlns:app="http://schemas.android.com/apk/res-auto"&nbsp; &nbsp; xmlns:tools="http://schemas.android.com/tools"&nbsp; &nbsp; android:id="@+id/drawer_layout"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; android:fitsSystemWindows="true"&nbsp; &nbsp; tools:openDrawer="start">&nbsp; &nbsp; <include&nbsp; &nbsp; &nbsp; &nbsp; layout="@layout/app_bar_main"&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/app_bar_main"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="match_parent" />&nbsp; &nbsp; <com.google.android.material.navigation.NavigationView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/nav_view"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_gravity="start"&nbsp; &nbsp; &nbsp; &nbsp; android:fitsSystemWindows="true"&nbsp; &nbsp; &nbsp; &nbsp; app:headerLayout="@layout/nav_header_main"&nbsp; &nbsp; &nbsp; &nbsp; app:menu="@menu/activity_main_drawer" /></androidx.drawerlayout.widget.DrawerLayout>

长风秋雁

您只需要设置内容视图,然后就可以通过mActivityMainBinding.appBarMain访问appbar。

湖上湖

通过视图绑定为包含的布局提供一个 id 会更容易代替<include layout="@layout/content_main" />只需使用<include&nbsp;&nbsp;android:id="@+id/contentmain"layout="@layout/content_main" />绑定将是 binding.contentmain

繁花不似锦

你可以尝试使用这个吗mAppBarMainBinding&nbsp;=&nbsp;DataBindingUtil.setContentView(this,&nbsp;R.layout.your_activity);对我来说效果很好。
随时随地看视频慕课网APP

相关分类

Java
我要回答