gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 16:27:32 26 4
gpt4 key购买 nike

我正在尝试为我的项目创建我自己的自定义 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 中完成?

最佳答案

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

<?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>

关于android - 在 android 中使用 ConstraintLayout 的自定义工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46500281/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com