首页 > 解决方案 > 是否能够处理包含的 xml 内的按钮按下

问题描述

是否能够处理包含的 xml 内的按钮按下(没有片段):

应用插件:'kotlin-android-extensions'

活动.kt

    leftBarButton.setOnClickListener {
        Log.d(TAG,"unaccessible!")
    }

活动.xml

<LinearLayout android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:background="@color/colorPrimaryDark"
              android:fitsSystemWindows="true">
    <include layout="@layout/navbar" 
             android:id="@+id/navBar"/>
</LinearLayout>

导航栏.xml

<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize">

        <LinearLayout android:orientation="horizontal"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:background="@color/colorPrimaryDark"
                      android:fitsSystemWindows="true"
                      android:layout_marginBottom="5pt"
                      android:gravity="bottom">
            <Button
                    android:id="@+id/leftBarButton"
                    android:layout_width="37dp"
                    android:layout_height="30dp"
                    android:layout_marginStart="@dimen/borderrmargin"
                    android:background="@drawable/ic_home"
                    android:layout_weight="0.1"/>

            <TextView
                    android:id="@+id/titleApp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="@color/colorTitleText"
                    android:textAlignment="center"
                    android:text="@string/title"
                    android:textSize="20sp"
                    android:layout_weight="0.8"/>
            <Button
                    android:id="@+id/rightBarButton"
                    android:layout_width="37dp"
                    android:layout_height="30dp"
                    android:layout_marginEnd="@dimen/borderrmargin"
                    android:background="@drawable/ic_home"
                    android:layout_weight="0.1"/>
        </LinearLayout>
    </RelativeLayout>
</merge>

onClickListener 不起作用

标签: androidandroid-layoutkotlin

解决方案


嘿,在您在包含的 xml 中添加合并标签之前,包含不起作用,尝试将您包含的 xml 包装在合并标签中。


推荐阅读