首页 > 解决方案 > Android 自定义工具栏不可点击

问题描述

我创建了一个自定义工具栏,左侧有一个“取消”按钮,写入上有一个“发布”按钮,这是大多数社交媒体应用程序使用的标准格式。

这是我的 xml 代码的一部分:

<androidx.appcompat.widget.Toolbar
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    style="@style/BarTheme"
    app:theme="@style/BarTheme"
    app:popupTheme="@style/AppTheme"
    app:layout_constraintTop_toTopOf="parent"
    android:background="@color/white"
    app:title="Lend"
    android:elevation="4dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/add_item_cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="left|center_vertical"
            android:textSize="15dp"
            android:fontFamily="sans-serif-condensed-light"
            android:background="@null"
            android:textColor="@color/grey"
            android:text="CANCEL"></Button>

        <TextView
            android:id="@+id/add_item_post"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:fontFamily="sans-serif-condensed-light"
            android:layout_gravity="bottom"
            android:textStyle="bold"
            android:gravity="center|center_vertical"
            android:textSize="30dp"
            android:text="NEW ITEM"></TextView>


        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right|center_vertical"
            android:layout_weight="1"
            android:background="@null"
            android:fontFamily="sans-serif-condensed-light"
            android:textSize="15dp"
            android:layout_marginRight="16dp"
            android:textColor="@color/themeBlue"
            android:textStyle="bold"
            android:text="POST"></Button>
    </LinearLayout>
</androidx.appcompat.widget.Toolbar>

这是我的 style.xml 代码:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/white</item>
    <item name="colorPrimaryDark">@color/white</item>
    <item name="colorAccent">@color/themeBlue</item>
</style>

<style name="BarTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/white</item>
    <item name="colorPrimaryDark">@color/white</item>
    <item name="colorAccent">@color/themeBlue</item>
</style>

</resources>

现在在活动中,我决定至少尝试“取消”按钮,但它不起作用。我试图调试,但它甚至没有进入函数,所以我认为它甚至不可点击。

@Override
public void onClick(View view) {
    switch(view.getId()) {
        case R.id.add_item_cancel:
            Intent goBackIntent = new Intent(AddItemActivity.this, ListingsActivity.class);
            startActivity(goBackIntent);

    }
}

提前感谢您的帮助!

标签: androiduser-interfaceandroid-xml

解决方案


我相信您正在使用onClickListener实现作为调用按钮的活动的一部分,但是您需要注册一个回调以在单击按钮时调用。

将以下代码行添加到onCreate按钮所在的 Activity 中的函数中:

protected void onCreate(Bundle savedValues) {
    ...
    Button button = (Button)findViewById(R.id.add_item_cancel);
    button.setOnClickListener(this);
}

实现OnClickListener回调:

public void onClick(View v) {
    switch(view.getId()) {
        case R.id.add_item_cancel:
            // do something when the button is clicked
    }
}

也可以看看:


推荐阅读