简体   繁体   English

FloatingActionButton正在占用屏幕的宽度

[英]FloatingActionButton is taking up the width of the screen

More Android Layout woes for the android newbie. 适用于android新手的更多Android布局问题。 I have been trying to learn android programming, and for the most part it is easy(ish), the hardest part is the layout. 我一直在尝试学习android编程,并且大多数情况下很容易,最难的部分是布局。 I have a Tab layout right now that is the "home" view, and on that view I need a floating action button. 我现在有一个Tab布局,它是“主页”视图,在该视图上我需要一个浮动操作按钮。 For some reason the floating action button is taking up the entire width of the screen. 由于某种原因,浮动操作按钮占用了整个屏幕宽度。 The button itself is the correct size, however the layout cuts off the bottom of the screen. 按钮本身的大小正确,但是布局会截断屏幕底部。 I have followed a few tutorials and no matter what I do it always takes the entire width cutting off the bottom of my screen. 我遵循了一些教程,无论我做什么,总会占用整个屏幕底部的宽度。 Screenshot attached of it running in an emulator from Android Studio. 附加的屏幕快照,它在Android Studio的模拟器中运行。 The layout is here... 布局在这里...

<?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>

I am filling the view with Fragments and such, but I don't think that would be the problem. 我正在用Fragments等填充视图,但是我认为这不是问题。 Can anyone see why the FloatingActionButton isn't floating, but instead acting as a full linear layout? 谁能看到为什么FloatingActionButton不浮动,而是充当完整的线性布局吗? Thank you so much for the help. 非常感谢你的帮助。

在此处输入图片说明

use coordinate Layout for showing floating action button: 使用坐标布局显示浮动操作按钮:

Remove floating action Button from your XML code and your XML code should be like this : 从您的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>

You need tu add the FAB only in the home layout: 您只需要在主页布局中添加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"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM