代码之家  ›  专栏  ›  技术社区  ›  sagar suri

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

  •  2
  • sagar suri  · 技术社区  · 9 年前

    我正在努力创造自己的习惯 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>
    

    ?

    2 回复  |  直到 9 年前
        1
  •  3
  •   John Le    9 年前

    我写了一个简单的布局。您可以查看我的代码以改进布局。希望有帮助:

    <?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>
    
        2
  •  2
  •   Silvio Guedes    9 年前

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

    <?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>
    
    推荐文章