2017-09-24 47 views
0

更多Android布局给android新手带来困扰。我一直在努力学习android编程,而且大多数情况下它很容易(ish),最难的部分就是布局。我现在有一个Tab布局,这是“家庭”视图,并在该视图上我需要一个浮动操作按钮。出于某种原因,悬浮动作按钮占据了屏幕的整个宽度。按钮本身是正确的大小,但是布局会切断屏幕的底部。我遵循了几个教程,不管我做什么,它总是将整个宽度切掉我的屏幕底部。在Android Studio的模拟器中运行的附件截图。 布局是这里...FloatingActionButton正在占用屏幕的宽度

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    tools:context="com.google.firebase.quickstart.auth.HomeActivity" 
    android:orientation="vertical"> 
    <!--<android.support.design.widget.AppBarLayout--> 
     <!--android:id="@+id/appbar"--> 
     <!--android:layout_width="match_parent"--> 
     <!--android:layout_height="wrap_content"--> 
     <!--android:paddingTop="8dp"--> 
     <!--android:theme="@style/AppTheme.AppBarOverlay">--> 
     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:minHeight="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay"> 

     </android.support.v7.widget.Toolbar> 
    <!--</android.support.design.widget.AppBarLayout>--> 
    <android.support.v4.view.ViewPager 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="@color/white" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 
    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:scaleType="center" 
     android:padding="8dp" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="8dp" 
     android:elevation="8dp" 
     android:tint="@color/white" 
     app:srcCompat="@drawable/ic_add"/> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/primaryLightColor" /> 
</LinearLayout> 

我填充片段和这样的观点,但我不认为这是问题。任何人都可以看到为什么FloatingActionButton不是浮动的,而是充当线性布局?十分感谢你的帮助。

enter image description here

+0

只是尝试我不确定完美的结果,但请让我知道它是否有效。在你floatingActionButton只是使** scaleType作为centreCrop **,然后看看它是否有效 –

+0

@AlokKumarVerma它没有工作,仍然显示整个屏幕上的按钮。谢谢 –

回答

1

使用协调布局显示浮动操作按钮:从你的XML代码

除去浮动作按钮,您的XML代码应该是这样的:

<?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" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="info.androidhive.fab.MainActivity"> 


    <include layout="@layout/your_current_layout" /> // your current xml code layout 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@android:drawable/ic_dialog_email" /> 

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

这是否会破坏我的布局,迫使标签到屏幕底部? –

+0

工作,但现在FAB覆盖屏幕底部的我的标签。此外,我只想要在“主页”选项卡上的晶圆厂,而不是任何其他选项卡。这会改变什么吗? –

+0

如果您想在主页选项卡中使用FAB,请将XML代码放在您的主页选项卡活动中,而不要放在父活动中。 –

0

您需要tu仅在家庭布局中添加FAB:

<android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@drawable/ic_add" 
     android:layout_gravity="bottom|end"/>