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

问题描述 投票:0回答:1

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

我需要允许触摸,如果用户触摸名称或图像附近的区域,它将改变活动状态。

示例:enter image description here

android android-actionbar toolbar
1个回答
2
投票

您只需要将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>

之后,您可以在活动中设置点击事件:

    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();
        }
    });

在模拟器上运行以查看结果:

example running on nexus 5

希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.