【问题标题】:<Include> an xml file with a fab button as it is<Include> 一个带有 fab 按钮的 xml 文件
【发布时间】:2018-12-01 10:46:37
【问题描述】:

我想在 xml 文件中包含另一个包含 fab 按钮的其他 xml 文件。问题是当我 &lt;include layout="@layout/fab 它在屏幕的左上角而不是右下角显示我的 fab 按钮。

这是我的 xml 文件(ConstraintLayout

<android.support.constraint.ConstraintLayout 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"
tools:context=".PlanPage.PlanPageView">


<include
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    layout="@layout/activity_fab_button"
    android:layout_gravity="end|bottom"/>

</android.support.constraint.ConstraintLayout>

这是fab按钮xml

<android.support.design.widget.FloatingActionButton
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
android:src="@drawable/ic_plusphoto_fab"
android:backgroundTint="@color/colorPrimary"
/>

【问题讨论】:

  • 你可以尝试在include中添加marginBottom吗??
  • 我添加了android:layout_marginBottom="200dp" 只是为了检查,它不起作用
  • 请尝试设置alignParant`bottom

标签: android xml include floating-action-button


【解决方案1】:

fab xml里面的这个属性:

android:layout_gravity="end|bottom"

将布局包含在另一个布局中时无效。
您必须为此布局设置属性:

<include
    layout="@layout/activity_fab_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent" />

如果它是有效的。
如果包含布局的宿主布局不是LinearLayout,那么你必须设置其他属性,比如 alignParentBottom 代表 RelativeLayout,或 将其约束到 ConstraintLayout 的底部和末尾。

【讨论】:

  • 当然要设置layout_heightlayout_widthwrap_content,然后根据主机布局设置合适的属性。
  • 不起作用:/它看起来很合法,但仍然不起作用:P
  • 发布主机布局
  • 好吧!
  • 你没有约束它。这是一个 ConstraintLayout 而不是 LinearLayout。
猜你喜欢
  • 2017-06-14
  • 1970-01-01
  • 2012-05-07
  • 1970-01-01
  • 2016-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-31
相关资源
最近更新 更多