我第一次使用 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"
/>
我在代码中向这个布局添加了一些视图。但他们的重力设置为最高。
这是它的外观:
片段代码:
@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" />