【问题标题】:How to force FAB go to the bottom of the layout?如何强制 FAB 到布局的底部?
【发布时间】:2016-01-06 14:40:46
【问题描述】:

我第一次使用 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" />

结果:

编辑 3:

【问题讨论】:

  • this 有帮助吗?
  • FAB 使用 CoordinatorLayout 到达底部(不过布局很酷),现在问题是它隐藏在图像视图后面。
  • 您可以发布更改吗?
  • 显然一切都在图像后面。包括我的文本视图。
  • 你是在RelativeLayout里面膨胀一个片段吗?

标签: android floating-action-button layout-gravity android-gravity


【解决方案1】:

线性布局将始终根据其方向从顶部或左侧开始布局视图。您需要将布局类型更改为相对布局或 CoordinatorLayout,以便您可以随意放置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-09
    • 1970-01-01
    • 2011-10-08
    • 1970-01-01
    • 2019-02-03
    相关资源
    最近更新 更多