在android中使用ConstraintLayout的自定义工具栏

时间:2017-09-30 06:43:29

标签: android android-constraintlayout

我正在尝试为我的项目创建自己的自定义Toolbar。目前我正在使用RelativeLayout来创建它。但我想避免Views的深层次结构。所以我想用ConstrainLayout创建它。如何在ConstraintLayout中实现以下目标。

enter image description here

以上布局的代码:

<RelativeLayout
            android:id="@+id/customAppBarLayout"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:paddingEnd="10dp"
            android:paddingStart="10dp">

            <ImageView
                android:id="@+id/toolbarCloseButton"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_centerVertical="true"
                android:adjustViewBounds="true"
                app:srcCompat="@drawable/ic_cancel" />

            <TextView
                android:id="@+id/toolbarTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginStart="15dp"
                android:layout_toEndOf="@+id/toolbarCloseButton"
                android:textColor="#000"
                android:textSize="18sp"
                tools:text="Puzzle" />

            <RelativeLayout
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_alignParentEnd="true"
                android:layout_centerVertical="true">

                <ImageView
                    android:id="@+id/coinIcon"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:paddingBottom="2dp"
                    android:paddingRight="2dp"
                    app:srcCompat="@drawable/ic_single_coin"
                    tools:ignore="MissingPrefix" />

                <TextView
                    android:id="@+id/prizeSize"
                    android:layout_width="16dp"
                    android:layout_height="16dp"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentEnd="true"
                    android:layout_centerInParent="true"
                    android:background="@drawable/circular_background"
                    android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:freezesText="true"
                    android:gravity="center"
                    android:marqueeRepeatLimit="marquee_forever"
                    android:maxEms="20"
                    android:maxLines="1"
                    android:padding="1dp"
                    android:scrollHorizontally="true"
                    android:textColor="#fff"
                    android:textSize="6sp"
                    tools:text="100" />
            </RelativeLayout>
        </RelativeLayout>

如何在ConstraintLayout中完成此操作?

2 个答案:

答案 0 :(得分:3)

我写了一个简单的布局。您可以看到我的代码来改善您的布局。希望它有所帮助:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#00ffff">

    <TextView
        android:id="@+id/tv"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#0000ff"
        android:gravity="center_vertical"
        android:text="TextView"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/iv_1"
        app:layout_constraintRight_toLeftOf="@+id/iv_2"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/iv_1"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:background="@android:color/holo_red_dark"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <ImageView
        android:id="@+id/iv_2"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:background="#00ff00"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

答案 1 :(得分:1)

您需要在工具栏代码中添加自定义布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolBar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/abc_action_bar_default_height_material"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" >

    <!-- your custom layout here --> 
    <include android:id="@+id/actionBar" layout="@layout/action_bar_layout"/>

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