gpt4 book ai didi

android - 创建和定位 float 操作按钮

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:02:18 26 4
gpt4 key购买 nike

我正在尝试将 FAB 添加到卡片 View 的底部,就像在这个应用程序中一样: app image

按钮确实显示了,但它不与卡片 View 重叠。我正在使用 Android Material Design Library .这是我的文件:

colors.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FFEB3B</color>
<color name="colorAccentDark">#FBC02D</color>
</resources>

activity_main.xml:

    tools:context=".MainActivity">

<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<LinearLayout
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/main_content"
android:layout_below="@id/toolbar">

<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/stop_card"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="5dp"
card_view:cardCornerRadius="4dp">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:paddingRight="10dp"
android:paddingBottom="5dp">

<TextView
android:id="@+id/label_stop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fermata"
android:textSize="25sp"
android:textStyle="bold"
android:singleLine="true" />

<EditText
android:id="@+id/edit_stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:layout_gravity="end" />
</LinearLayout>
</android.support.v7.widget.CardView>

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="72dp" >

<com.gc.materialdesign.views.ButtonFloat
android:id="@+id/buttonFloat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#1E88E5"
materialdesign:animate="true"
materialdesign:iconDrawable="@android:drawable/ic_menu_search"
android:layout_gravity="end"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />

</RelativeLayout>
</LinearLayout>
</RelativeLayout>

build.gradle:

apply plugin: 'com.android.application'

android {
compileSdkVersion 22
buildToolsVersion "22.0.1"

defaultConfig {
applicationId "com.example.john.gttime"
minSdkVersion 21
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.+'
compile 'com.android.support:cardview-v7:21.0.+'
compile 'com.android.support:recyclerview-v7:21.0.+'
compile 'com.github.navasmdc:MaterialDesign:1.+@aar'
}

几乎是对的,但应该在卡片 View 的中间。这是我在 Android Studio 预览中得到的:
result

最佳答案

您可以尝试向按钮或其父级添加负的上边距,以便向上移动。由于您似乎想要将它与卡片恰好重叠一半,您可以将 android:layout_marginTop="-36dp" 添加到按钮的父 RelativeLayout,其中 36dp 是一半它的高度:

    <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="72dp"
android:layout_marginTop="-36dp">

<com.gc.materialdesign.views.ButtonFloat
android:id="@+id/buttonFloat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#1E88E5"
materialdesign:animate="true"
materialdesign:iconDrawable="@android:drawable/ic_menu_search"
android:layout_gravity="end"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />

</RelativeLayout>

关于android - 创建和定位 float 操作按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29722250/

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