0

我第一次使用FAB。無論我使用什麼設置來使用重力和錨點屬性,FAB總是顯示在頂部。我想讓它出現在右下角。我該怎麼做到這一點?如何強制FAB進入佈局的底部?

我的佈局XML

<?xml version="1.0" encoding="utf-8"?><!-- DO NOT EVER CHANGE THIS LINEAR LAYOUT TO ANYTHING ELSE WITHOUT CHECKING PropertyDetailFragment.fillYourself cause it's used there to add children to it--> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:app="http://schemas.android.com/apk/res-auto" 
       android:id="@+id/content" 
       style="@style/StyleForVerticalLinearLayoutNoPadding" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
     > 
    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="16dp" 
     android:clickable="true" 
     android:src="@android:drawable/ic_dialog_map" 
     app:layout_anchorGravity="bottom|right|end" 
     android:layout_gravity="bottom|end" 
     /> 

我補充一些意見,這個佈局代碼。但他們的重力已經上升到了頂峯。

下面是它的外觀:

enter image description here

片段代碼:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    if (savedInstanceState != null && savedInstanceState.containsKey("propertyID")) 
     propertyID = savedInstanceState.getLong("propertyID"); 
    currentPhotoPosition = getActivity().getIntent().getExtras().getInt("currentPhotoPosition"); 
    final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_property_detail, container, false); 
    new PropertyDetailProxy().run(new Response.Listener<PropertyDetailed>() { 
     @Override 
     public void onResponse(PropertyDetailed propertyDetailed) { 
      if (propertyDetailed.getNumberOfPhotos() > 0) 
       new PhotosIDsProxy().run(new Response.Listener<long[]>() { 
        private long[] imagesIDs; 

        @Override 
        public void onResponse(long[] photoIDs) { 
         imagesIDs = photoIDs; 
         if (imagesIDs != null && imagesIDs.length > 0) { 
          PhotoAdapter adapter = new PhotoAdapter(getActivity()); 
          ViewPager viewPager = new ViewPager(rootView.getContext()); 
          rootView.addView(viewPager); 
          viewPager.setAdapter(adapter); 
          adapter.setPropertyImagesIDs(photoIDs); 
          adapter.notifyDataSetChanged(); 
          viewPager.setCurrentItem(currentPhotoPosition); 
         } 
        } 
       }, new DefaultErrorListener(), propertyID); 
      rootView.addView(new PropertyDetailedPersianStringMaker(propertyDetailed).make()); 
     } 
    }, new DefaultErrorListener(), propertyID); 
    return rootView; 
} 

編輯: 更改代碼:

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

<RelativeLayout 
     android:id="@+id/content" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

</RelativeLayout> 

<android.support.design.widget.FloatingActionButton 
     android:layout_margin="10dp" 
     android:id="@+id/myFAB" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:backgroundTint="#FF0000" 
     app:borderWidth="0dp" 
     app:elevation="8dp" 
     app:layout_anchor="@id/content" 
     app:layout_anchorGravity="bottom|right|end" /> 

結果: enter image description here

編輯3: enter image description here

+1

does [this](http://stackoverflow.com/questions/31088453/how-to-make-android-support-floatingactionbutton-at-bottom-right-of-the-screen)help? – Blackbelt

+0

FAB使用CoordinatorLayout(儘管很酷的佈局)到底部,問題現在它隱藏在圖像視圖後面。 – Tina

+0

你可以發佈更改嗎? – Blackbelt

回答

1

線性佈局將始終佈局從頂部或依賴於它的方向的左邊開始的意見。您需要將佈局類型更改爲相對佈局或CoordinatorLayout,以便您可以根據需要定位它。