问题
I am trying to create Android product layout. In footer have two buttons it is fixed button. you can see images when I am scrolling up bottom two buttons attached in my layout. Please help how can i do?
Image 1
Image 2
after scrolling
xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:id="@+id/coordinate"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.chivazo.chivazoandroid.activities.SingleProductActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginBottom="30dp"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:src="@drawable/a"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_single_product" />
<LinearLayout
android:id="@+id/groupbutton"
android:weightSum="2"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.v7.widget.AppCompatButton
android:layout_weight="1"
android:id="@+id/share"
android:text="share"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.AppCompatButton
android:layout_weight="1"
android:id="@+id/go_wishlist"
android:text="go to Wishlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
回答1:
You can use Relativelayout
as your outer layout and keep CoordinatorLayout
inside it, make Two buttons android:layout_alignParentBottom="true"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:orientation="vertical">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinate"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/groupbutton"
android:fitsSystemWindows="true"
tools:context="com.chivazo.chivazoandroid.activities.SingleProductActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginBottom="30dp"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:src="@drawable/a"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_single_product" />
</android.support.design.widget.CoordinatorLayout>
<LinearLayout
android:id="@+id/groupbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:weightSum="2">
<android.support.v7.widget.AppCompatButton
android:id="@+id/share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="share" />
<android.support.v7.widget.AppCompatButton
android:id="@+id/go_wishlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="go to Wishlist" />
</LinearLayout>
</RelativeLayout>
回答2:
Simply add your layout in Coordinatorlayout below NestedScrollView in xml and do the following in java.
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) layoutToBeAnchored.getLayoutParams();
layoutParams.setAnchorId(R.id.layoutBelowWhichToBeAnchored);
layoutParams.anchorGravity = Gravity.CENTER;
layoutToBeAnchored.setLayoutParams(layoutParams);
Hope this helps you guys. For any reference you can check the example at - https://github.com/IsUncommon/Droidcon-India-2015
回答3:
The app:layout_anchor="@id/anchor_view"
and
app:layout_anchorGravity="bottom"
are the Important attributes here
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".ProductDetailActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/nested_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
.
.
.
<View
android:id="@+id/anchor_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
.
.
.
.
</android.support.v4.widget.NestedScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_anchor="@id/anchor_view"
app:layout_anchorGravity="bottom">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
回答4:
If your included layout layout="@layout/content_single_product"
contains a NestedScrollView
as a root view container. Try to add an id to it like @+id/nestedView
with android:layout_height="match_parent"
, You must probably add bottom padding with the same height value of your buttons , and Just keep the rest as it is, and replace android:layout_gravity="bottom"
inside your bottom linear layout which contains the two buttons with :
app:layout_anchor="@id/nestedView"
app:layout_anchorGravity="bottom"
回答5:
<RelativeLayout
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="match_parent">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleGravity="center_vertical|start"
app:expandedTitleTextAppearance="@android:color/transparent"
app:layout_collapseMode="parallax"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout
android:id="@+id/lay_image_slider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/browseCategories"
android:background="@color/white">
<FrameLayout
android:id="@+id/rel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="vertical">
<com.example.android_dev.ecm.View.ViewPagerCustomDuration
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="300dp" />
<com.example.android_dev.ecm.View.CirclePageIndicator
android:id="@+id/indicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@android:color/transparent"
android:padding="10dp" />
</FrameLayout>
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbarSeller"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="56dp"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include
android:id="@+id/contentScroll"
layout="@layout/content_scroll_product_detail" />
</android.support.design.widget.CoordinatorLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="8dp"
android:layout_gravity="center"
android:background="@drawable/shadow" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="@+id/addToCart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/white"
android:text="Add To Cart"
android:textColor="@color/black"
android:textSize="16sp" />
<!--<Button
android:id="@+id/addToWishList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/colorAccent"
android:text="Add To Wishlist"
android:textColor="@color/white" />-->
<Button
android:id="@+id/buyNow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/colorPrimary"
android:text="Buy Now"
android:textColor="@color/white"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
content_scroll_product_detail.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:expandableTextView="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="com.example.android_dev.ecm.Helper.FixedScrollingViewBehaviour">
<android.support.v4.widget.NestedScrollView
android:id="@+id/nestedScroll"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="@+id/productDetails"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/productTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:textColor="@color/black"
android:textSize="18sp" />
<TextView
android:id="@+id/productName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="2dp"
android:textSize="16sp" />
<RelativeLayout
android:id="@+id/priceLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/productDiscountPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/productPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/productDiscountPrice"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:textColor="@color/gray"
android:textSize="14sp" />
<LinearLayout
android:id="@+id/ratingProduct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:orientation="horizontal">
<TextView
android:id="@+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:padding="10dp"
android:text="3.2"
android:textColor="@color/colorAccent"
android:textSize="22sp" />
<RatingBar
android:id="@+id/star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/rating"
android:isIndicator="true"
android:numStars="1"
android:rating="1" />
</LinearLayout>
<LinearLayout
android:id="@+id/sold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/productPrice"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sold by : "
android:textColor="@color/gray"
android:textSize="14sp" />
<TextView
android:id="@+id/soldBy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/gray"
android:textSize="14sp" />
</LinearLayout>
<TextView
android:id="@+id/orders"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/sold"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:text="Additional tax may apply charged at checkout"
android:textSize="14sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dp"
android:weightSum="1">
<Button
android:id="@+id/share"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.5"
android:background="@drawable/lay_border"
android:drawableLeft="@drawable/shareic"
android:drawablePadding="-20dp"
android:gravity="center"
android:paddingLeft="8dp"
android:text="Share"
android:textColor="@color/black" />
<Button
android:id="@+id/addToWishList"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.5"
android:background="@drawable/lay_border"
android:drawableLeft="@drawable/pinic"
android:drawablePadding="-10dp"
android:gravity="center"
android:paddingLeft="8dp"
android:text="Add To Pinlist"
android:textColor="@color/black" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/ratingSeller"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/productDetails">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp">
</RelativeLayout>
</android.support.v7.widget.CardView>
<TextView
android:id="@+id/outOfStock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ratingSeller"
android:background="@color/light_red"
android:gravity="center"
android:text="Sorry this product is out of stock."
android:textColor="@color/white"
android:textSize="22sp"
android:visibility="gone" />
<android.support.v7.widget.CardView
android:id="@+id/productVariants"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/outOfStock"
android:layout_marginTop="5dp">
<LinearLayout
android:id="@+id/addAttributesHere"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</android.support.v7.widget.CardView>
<LinearLayout
android:id="@+id/saveButtons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/productVariants"
android:orientation="horizontal"
android:visibility="gone"
android:weightSum="2">
<Button
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/black"
android:text="Add To Cart"
android:textColor="@color/white" />
<!-- <Button
android:id="@+id/save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/colorAccent"
android:text="Add To Wishlist"
android:textColor="@color/white" />-->
<Button
android:id="@+id/buyNow2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/colorAccent"
android:text="Buy Now"
android:textColor="@color/white" />
</LinearLayout>
<android.support.v7.widget.CardView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/saveButtons"
android:layout_marginTop="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="Description"
android:textColor="@color/black"
android:textSize="16sp"
android:textStyle="bold" />
<com.ms.square.android.expandabletextview.ExpandableTextView
android:id="@+id/expand_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
expandableTextView:animDuration="200"
expandableTextView:maxCollapsedLines="2">
<TextView
android:id="@+id/expandable_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:textColor="@color/black"
android:textSize="14sp" />
<ImageButton
android:id="@id/expand_collapse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:background="@android:color/transparent"
android:padding="16dp" />
</com.ms.square.android.expandabletextview.ExpandableTextView>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/recommend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/description"
android:layout_marginTop="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<TextView
android:id="@+id/recommendHead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Recommendation"
android:textColor="@color/black"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/recommendText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/recommendHead"
android:padding="10dp"
android:text="Would you like to recommend this product?"
android:textColor="@color/gray"
android:textSize="14sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/recommendText"
android:layout_marginBottom="5dp"
android:background="@drawable/lay_border"
android:weightSum="2">
<Button
android:id="@+id/yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:background="#deffffff"
android:text="Yes"
android:textColor="@color/colorPrimary"
android:textSize="18sp" />
<Button
android:id="@+id/no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1"
android:background="#f7f5f5"
android:text="No"
android:textColor="@color/colorPrimary"
android:textSize="18sp" />
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/productSellers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/recommend"
android:layout_marginTop="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/addSellersHere"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</RelativeLayout>
来源:https://stackoverflow.com/questions/36980503/android-nestedscrollview-bottom-two-button-attached