Android 可点击头像和名称的工具栏

Android Toolbar with Avatar and Name clickable

我正在寻找一种创建工具栏的方法,喜欢很多带有名称和照片的应用程序。

我需要允许触摸,如果用户触摸名称或图像附近的区域,它会改变 activity。

示例:

你只要把ImageViewTextView放在你的Toolbar里,他就是一个ViewGroup。例如:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="@dimen/abc_action_bar_default_height_material">

    <LinearLayout
            android:id="@+id/linearlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/your_image_description"
            android:src="@drawable/your_image"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/your_string" />

    </LinearLayout>

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

之后,您可以在 activity 上设置点击事件:

    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearlayout);
    linearLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(MainActivity.this, "teste", Toast.LENGTH_LONG).show();
        }
    });

运行在模拟器上看结果:

希望对您有所帮助。