android - 将 ImageButton 放在两个视图组之间
问题描述
我有LinearLayout
一个Toolbar
里面有一个和一个EditText
和一个Button
。我正在使用图像来表达我想要的,这很简单:
这就是我现在所拥有的:(ImageButton
带有徽标的在Toolbar
)
这就是我想要实现的目标:(ImageButton
带有标志的在Toolbar
和之间LinearLayout
)
这是我的布局文件的 XML 代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/secondColor"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/mainColor"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:paddingLeft="-10dp">
<ImageButton
android:id="@+id/btnMenu"
style="@android:style/Widget.DeviceDefault.ImageButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:background="@null"
android:src="@drawable/menu_principal" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:layout_weight="5"
android:fontFamily="@font/roboto_medium"
android:text="@string/app_name"
android:textAlignment="center"
android:textColor="@android:color/white"
android:textSize="24sp" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="false"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:background="@null"
android:scaleType="fitXY"
android:src="@drawable/spotify_logo" />
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.Toolbar>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="@string/editReminderTitleHint"
/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
解决方案
您可以通过将LinearLayout
顶级视图更改为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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<EditText
android:id="@+id/edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:hint="Reminder title"/>
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@+id/edit"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:text="BUTTON"/>
<ImageButton
android:layout_width="72dp"
android:layout_height="72dp"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="@+id/toolbar"
app:layout_constraintEnd_toEndOf="parent"/>
</android.support.constraint.ConstraintLayout>
这里重要的部分是您将 的顶部和底部都ImageButton
约束到Toolbar
. 这将使它在工具栏的底部边缘居中。
请注意,我没有为我的示例 ImageButton 设置特殊背景,但您可以将其设为圆形,它会正常工作。
推荐阅读
- python-3.x - ModuleNotFoundError:没有名为“instabot”的模块
- c# - 如何为方法设置参数?
- java - RichTextFX 行号列扩展到整个文本区域
- android - SimpleDateFormat 解析不适用于数据库中 UTC 格式的日期
- android - 带有 CollapsingToolbarLayout 的回收站视图正常工作
- apache-zookeeper - 无法使用 docker-druid 映像向 druid 发送数据
- java - 我可以将 Java 接口方法实现为私有或受保护的吗?
- r - 在R中为背靠背条形图悬停
- node.js - React 身份验证路由在生产构建(NodeJS)中不起作用
- c++ - 使用 mmap 读取选定的文件行