简体   繁体   English

将ImageButton放在两个视图组之间

[英]Put ImageButton between two view groups

I have a LinearLayout with a Toolbar inside it and an EditText and a Button . 我有一个带有ToolbarLinearLayout和一个EditText和一个Button I'm using images to express what i want, it is simple: 我正在使用图像表达我想要的东西,这很简单:

This is what I have right now: (the ImageButton with the logo is in the Toolbar ) 这就是我现在所拥有的:(带有徽标的ImageButtonToolbar

在此处输入图片说明

And this is what I want to achieve: (The ImageButton with the logo is between the Toolbar and the LinearLayout ) 这就是我要实现的目标:(带有徽标的ImageButtonToolbarLinearLayout

在此处输入图片说明

This is my XML code for the layout file: 这是我的布局文件的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>

You can achieve this by changing your LinearLayout top-level view into a ConstraintLayout : 您可以通过将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>

The important part here is that you constrain both the top and bottom of the ImageButton to the bottom of the Toolbar . 这里的重要部分是将ImageButton的顶部和底部限制在Toolbar的底部。 This will center it on the bottom edge of the toolbar. 这会将其居中在工具栏的底部边缘。

在此处输入图片说明

Note that I didn't set a special background for my example ImageButton, but you can make it a circle and it will work just fine. 请注意,我没有为示例ImageButton设置特殊的背景,但是您可以将其设置为圆形,这样就可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将视图置于两个具有严格尺寸的视图之间 - Put view between two Views with strict size 如何动态地在两个LinearLayout之间放置视图? - How to put the View dynamicaly between two LinearLayout? 在 Recycler List View 中在 ImageButton 的两种外观之间切换的最佳实践:setImageBitmap vs setImageResource vs setImageDrawable - Best practice for changing between two appearances of an ImageButton in Recycler List View: setImageBitmap vs setImageResource vs setImageDrawable 如何在android中的多行文本视图之间放置一个imagebutton/imageview - How to put a imagebutton/imageview between a multiline textview in android 如何在组之间的导航视图中删除行 - How to remove line in navigation view between groups 自定义ImageButton视图 - Custom ImageButton View 如何将TextView放在ImageButton的中心? - How to put a TextView in the center of a ImageButton? 将imageButton放在textview中的文本中间 - Put an imageButton in the middle of the text in the textview 两个图像作为ImageButton中的背景? - Two Images as background in ImageButton? 如何在相对布局中将ImageButton放在居中的ImageButton上方 - How to put an ImageButton above a centered ImageButton in a relative layout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM