在 Lollipop 之前的 CardView 下方的浮动操作按钮
Floating Action Button going below CardView on pre Lollipop
我正在开发 android 应用程序。其中有两张牌,两张牌之间有一张 FAB。屏幕在 API 21 下看起来很完美,但在 21 下,按钮的下半部分在第二个 card.Two 图像下方已附加:PREVIEW BEFORE API 21 and PREVIEW ON API 21。请帮助。谢谢!
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:background="@color/colorAccent"
android:layout_height="5dip"
android:layout_gravity="start"
android:layout_marginBottom="10dp"
android:layout_width="match_parent" />
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="05dp">
<android.support.v7.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/header"
app:cardUseCompatPadding="true"
android:layout_marginStart="-4dp"
android:layout_marginLeft="-4dp"
android:layout_marginBottom="-4dp"
android:layout_marginTop="-4dp"
android:layout_marginRight="-4dp"
android:layout_marginEnd="-4dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:elevation="500dp"
app:cardCornerRadius="5dp">
//First CARDVIEW ELEMENTS
</android.support.v7.widget.CardView>
<android.support.design.widget.FloatingActionButton
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/action_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardUseCompatPadding="true"
android:clickable="true"
android:onClick="SampleClick"
android:layout_alignParentRight="true"
android:src="@mipmap/create_button"
android:layout_alignBottom="@id/header"
android:layout_marginBottom="-32dp"
android:layout_marginRight="20dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_below="@id/header">
<View
android:background="@color/colorAccent"
android:layout_height="5dip"
android:layout_gravity="start"
android:layout_width="match_parent" />
<android.support.v7.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/footer"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
app:cardUseCompatPadding="true"
android:layout_marginStart="-4dp"
android:layout_marginLeft="-4dp"
android:layout_marginBottom="-4dp"
android:layout_marginTop="-4dp"
android:layout_marginRight="-4dp"
android:layout_marginEnd="-4dp"
android:elevation="500dp"
app:cardCornerRadius="5dp">
//SECOND CARDVIEW ELEMENTS
</android.support.v7.widget.CardView>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
您可以通过向您的 FloatingActionButton 添加 layout_anchor
标签来实现,这意味着在该视图上绘制我的浮动内容,这需要您将顶部布局也更改为 CoordinatorLayout
请看
<android.support.design.widget.CoordinatorLayout
android:id="@+id/main_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" // add this line
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/header"
app:cardUseCompatPadding="true"
android:layout_marginStart="-4dp"
android:layout_marginLeft="-4dp"
android:layout_marginBottom="-4dp"
android:layout_marginTop="-4dp"
android:layout_marginRight="-4dp"
android:layout_marginEnd="-4dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:elevation="500dp"
app:cardCornerRadius="5dp">
//First CARDVIEW ELEMENTS
</android.support.v7.widget.CardView>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="16dp"
android:src="@drawable/ic_done"
app:layout_anchor="@id/header" // whatever you want
app:layout_anchorGravity="bottom|right|end" />
<android.support.v7.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/footer"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
app:cardUseCompatPadding="true"
android:layout_marginStart="-4dp"
android:layout_marginLeft="-4dp"
android:layout_marginBottom="-4dp"
android:layout_marginTop="-4dp"
android:layout_marginRight="-4dp"
android:layout_marginEnd="-4dp"
android:elevation="500dp"
app:cardCornerRadius="5dp">
//SECOND CARDVIEW ELEMENTS
</android.support.v7.widget.CardView>
</android.support.design.widget.CoordinatorLayout>
我正在开发 android 应用程序。其中有两张牌,两张牌之间有一张 FAB。屏幕在 API 21 下看起来很完美,但在 21 下,按钮的下半部分在第二个 card.Two 图像下方已附加:PREVIEW BEFORE API 21 and PREVIEW ON API 21。请帮助。谢谢!
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:background="@color/colorAccent"
android:layout_height="5dip"
android:layout_gravity="start"
android:layout_marginBottom="10dp"
android:layout_width="match_parent" />
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="05dp">
<android.support.v7.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/header"
app:cardUseCompatPadding="true"
android:layout_marginStart="-4dp"
android:layout_marginLeft="-4dp"
android:layout_marginBottom="-4dp"
android:layout_marginTop="-4dp"
android:layout_marginRight="-4dp"
android:layout_marginEnd="-4dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:elevation="500dp"
app:cardCornerRadius="5dp">
//First CARDVIEW ELEMENTS
</android.support.v7.widget.CardView>
<android.support.design.widget.FloatingActionButton
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/action_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardUseCompatPadding="true"
android:clickable="true"
android:onClick="SampleClick"
android:layout_alignParentRight="true"
android:src="@mipmap/create_button"
android:layout_alignBottom="@id/header"
android:layout_marginBottom="-32dp"
android:layout_marginRight="20dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_below="@id/header">
<View
android:background="@color/colorAccent"
android:layout_height="5dip"
android:layout_gravity="start"
android:layout_width="match_parent" />
<android.support.v7.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/footer"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
app:cardUseCompatPadding="true"
android:layout_marginStart="-4dp"
android:layout_marginLeft="-4dp"
android:layout_marginBottom="-4dp"
android:layout_marginTop="-4dp"
android:layout_marginRight="-4dp"
android:layout_marginEnd="-4dp"
android:elevation="500dp"
app:cardCornerRadius="5dp">
//SECOND CARDVIEW ELEMENTS
</android.support.v7.widget.CardView>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
您可以通过向您的 FloatingActionButton 添加 layout_anchor
标签来实现,这意味着在该视图上绘制我的浮动内容,这需要您将顶部布局也更改为 CoordinatorLayout
请看
<android.support.design.widget.CoordinatorLayout
android:id="@+id/main_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" // add this line
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/header"
app:cardUseCompatPadding="true"
android:layout_marginStart="-4dp"
android:layout_marginLeft="-4dp"
android:layout_marginBottom="-4dp"
android:layout_marginTop="-4dp"
android:layout_marginRight="-4dp"
android:layout_marginEnd="-4dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:elevation="500dp"
app:cardCornerRadius="5dp">
//First CARDVIEW ELEMENTS
</android.support.v7.widget.CardView>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="16dp"
android:src="@drawable/ic_done"
app:layout_anchor="@id/header" // whatever you want
app:layout_anchorGravity="bottom|right|end" />
<android.support.v7.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/footer"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
app:cardUseCompatPadding="true"
android:layout_marginStart="-4dp"
android:layout_marginLeft="-4dp"
android:layout_marginBottom="-4dp"
android:layout_marginTop="-4dp"
android:layout_marginRight="-4dp"
android:layout_marginEnd="-4dp"
android:elevation="500dp"
app:cardCornerRadius="5dp">
//SECOND CARDVIEW ELEMENTS
</android.support.v7.widget.CardView>
</android.support.design.widget.CoordinatorLayout>