Android 工具栏自定义布局元素点击事件

Android toolbar custom layout element click event

我已经为我的项目创建了自定义工具栏。工具栏工作正常并显示 .这是工具栏的代码

toolbar.xml

    android:elevation="4dp">

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

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:layout_gravity="center_vertical">

            <ImageView
            android:id="@+id/imageToolbal"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:src="@drawable/alert_on"
            android:layout_weight="4"/>

            <TextView
            android:id="@+id/textToolbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Test"
            android:layout_weight="4"/>

        </LinearLayout>

    </LinearLayout>

</android.support.v7.widget.Toolbar>

主要布局activity

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.app.MainActivity">

    <include layout="@layout/toolbar" />

</RelativeLayout>

在我的onCreate代码方法中,我设置了这样的工具栏

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    } 

它按预期显示,但我无法为工具栏中的布局触发单击事件。就像我想显示一个 Toast 如果单击工具栏中 ID 为 textToolbartextView,或者我想有条件地隐藏和显示它。我该怎么做?

这样的工具栏操作。

       Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        // Title and subtitle
        toolbar.setTitle(R.string.about_toolbar_title);
        toolbar.setSubtitleTextColor(Color.WHITE);
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setBackgroundColor(getResources().getColor(
                R.color.themeToolbarColor));
        toolbar.setNavigationIcon(R.drawable.ic_action_back);
        toolbar.setNavigationOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
               // finish();
            }
        });
        toolbar.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {


            }
        });

您可以如下获取工具栏文本视图:

  TextView textToolbar= (TextView)toolbar.findViewById(R.id.textToolbar);

  textToolbar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

         // YOUR TOAST HERE
        }
    });

隐藏和显示如下:

    if(YOUR_CONDITION){
       textToolbar.setVisibility(View.VISIBLE);
    }else{
       textToolbar.setVisibility(View.GONE);
    }

您可以为要单击的 textView 分配一个 xml onClick 事件,然后在 Activity.

中实现该事件

xml 就像

<TextView
            android:id="@+id/textToolbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Test"
            android:onclick="test"
            android:layout_weight="4"/>

实现 onclick 事件的方法是,

public void test(View V){
   //onclick event statement
}

//在 "res/styles.xml"

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

// 在 activity

public class MainActivity extends AppCompatActivity {

Toolbar mToolbar;
TextView txtToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mToolbar = (Toolbar) findViewById(R.id.toolBar);
    //textView inside toolbar
    txtToolbar = (TextView) findViewById(R.id.txtToolbar);
    setSupportActionBar(mToolbar);
}

@Override
protected void onResume() {
    super.onResume();
    txtToolbar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("Toolbar Item","TxtClicked");
        }
    });
}

}

//工具栏

<android.support.v7.widget.Toolbar
    android:id="@+id/toolBar"
    android:background="@color/colorPrimary"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/txtToolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ToolBar TextView"/>
</android.support.v7.widget.Toolbar>