2015-07-06 45 views
5

Tôi muốn Tạo Layout như Image Sau với Material CardView và Custom Action Floating Button: enter image description hereTạo Layout với CardView và Floating Action Nút Android

Và tôi đã được phát triển tối đa tập tin xml Layout này như sau:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 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" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <RelativeLayout 
     android:id="@+id/mainview" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <android.support.v7.widget.CardView 
      android:id="@+id/cv" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:cardCornerRadius="2dp" 
      app:cardElevation="2dp" 
      android:layout_marginBottom="@dimen/cardMarginVertical" 
      android:layout_marginLeft="@dimen/cardMarginHorizontal" 
      android:layout_marginRight="@dimen/cardMarginHorizontal" 
      android:layout_marginTop="@dimen/cardMarginVertical" 
      app:cardPreventCornerOverlap="false" 
      app:contentPadding="0dp"> 

      <com.github.florent37.materialviewpager.sample.ExpandableTextView 
       android:id="@+id/expandingTextView" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:textSize="15dp" 
       android:layout_marginLeft="10dp" 
       android:layout_marginTop="10dp" 
       android:layout_marginRight="10dp" 
       android:layout_marginBottom="10dp" 
       android:text="@string/longText" /> 

      <RelativeLayout 
       android:id="@+id/buttonlayout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/expandingTextView" 
       android:layout_gravity="bottom" 
       android:background="@android:color/transparent"> 

       <com.getbase.floatingactionbutton.FloatingActionButton 
        android:id="@+id/action_c" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        fab:fab_colorNormal="#fff" 
        android:layout_alignParentRight="true" 
        fab:fab_colorPressed="#fff" 
        android:visibility="visible" /> 
      </RelativeLayout> 
     </android.support.v7.widget.CardView> 
    </RelativeLayout> 
</FrameLayout> 
+0

Xin hãy giúp tôi trong Layout này tôi đã nhầm lẫn như làm việc trên Layout này với 2 ngày nhưng không thể hoàn thành việc bố trí lên đến bức ảnh này. –

Trả lời

10

Để triển khai giao diện người dùng như ảnh của bạn, một số mã như dưới đây có thể hữu ích.

Tham số layout_anchorlayout_anchorGravity trong FloatingActionButton là điểm.

<?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" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

    <android.support.v4.widget.NestedScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

     <android.support.v7.widget.CardView 
      android:id="@+id/cv" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="16dp"> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="The Android Support Library package is a set of code libraries that provide backward-compatible versions of Android framework APIs as well as features that are only available through the library APIs. Each Support Library is backward-compatible to a specific Android API level. This design means that your applications can use the libraries' features and still be compatible with devices running Android 1.6 (API level 4) and up." /> 

     </android.support.v7.widget.CardView> 

    </android.support.v4.widget.NestedScrollView> 

    <android.support.design.widget.FloatingActionButton 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     app:layout_anchor="@id/cv" 
     app:layout_anchorGravity="right|end|bottom" 
     android:layout_margin="16dp" 
     android:clickable="true"/> 

</android.support.design.widget.CoordinatorLayout> 

Edit:

câu trả lời sớm đã có một số lỗi trong Android 5.0.1 và 5.0.2, FAB sẽ hiển thị bên dưới CardView.

Thêm NestedScrollView với layout_behavior ngoài CardView để làm việc trong Android 5.0.1 và 5.0.2

+0

Xin chào Ellie tôi sẽ thử và thông báo cho bạn sớm ... –

+0

Bạn có thể thực hiện thay đổi mã trong mã đã đăng của mình như trên để tôi có thể thực hiện dễ dàng không. Cám ơn vì hỗ trợ. –

+1

Tôi đã xóa một số mã không hữu ích khỏi bố cục của mình. Bạn có thể xem qua. –

Các vấn đề liên quan