Android 如何在选项卡布局中在屏幕上设置FloatingActionButton?

Android 如何在选项卡布局中在屏幕上设置FloatingActionButton?,android,fragment,Android,Fragment,在我的应用程序中,我想在选项卡布局的屏幕上设置FloatingActionButton 在我的项目中,我首先创建了TableLayout和ViewPager,最后创建了FloatingActionButton,就像图片一样 访问\u tab.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/

在我的应用程序中,我想在选项卡布局的屏幕上设置FloatingActionButton

在我的项目中,我首先创建了TableLayout和ViewPager,最后创建了FloatingActionButton,就像图片一样

访问\u tab.xml

    <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.Weal.sachin.omcom.TabFragment"
    android:orientation="vertical">


    <android.support.design.widget.TabLayout
        android:id="@+id/sliding_tabs_visit"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        app:tabTextColor="#000"
        style="@style/Base.TextAppearance.AppCompat.Light.Widget.PopupMenu.Large"
        app:tabSelectedTextColor="#fff"
        android:background="#c64c24"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager_visit"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/white"/>
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab111"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_gravity="bottom|end"
        android:backgroundTint="@color/cardview_dark_background"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_input_add"/>
</LinearLayout>
在这个xml文件中,floatinActionButton显示得非常完美,但它取代了ViewPager,但我想在ViewPager上设置floatinActionButton。

获得了以下解决方案:-

xml中几乎没有变化请访问\u tab.xml

<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.Weal.sachin.omcom.TabFragment"
    android:orientation="vertical">


    <android.support.design.widget.TabLayout
        android:id="@+id/sliding_tabs_visit"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        app:tabTextColor="#000"
        style="@style/Base.TextAppearance.AppCompat.Light.Widget.PopupMenu.Large"
        app:tabSelectedTextColor="#fff"
        android:background="#c64c24"/>
    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager_visit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"/>
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab111"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_gravity="bottom|end"
            android:backgroundTint="@color/cardview_dark_background"
            android:layout_margin="@dimen/fab_margin"
            android:src="@android:drawable/ic_input_add"/>
        </android.support.design.widget.CoordinatorLayout>

</LinearLayout>

用以下内容替换布局:

<RelativeLayout 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:orientation="vertical"
                tools:context="com.Weal.sachin.omcom.TabFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.design.widget.TabLayout
            android:id="@+id/sliding_tabs_visit"
            style="@style/Base.TextAppearance.AppCompat.Light.Widget.PopupMenu.Large"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:background="#c64c24"
            app:tabSelectedTextColor="#fff"
            app:tabTextColor="#000"/>

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager_visit"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/white"/>
    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab111"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_margin="@dimen/fab_margin"
        android:backgroundTint="@color/cardview_dark_background"
        android:src="@android:drawable/ic_input_add"/>
</RelativeLayout>

以下是在视图顶部添加FloatingActionButton的示例代码

 FrameLayout frameLayout = (FrameLayout)findViewById(android.R.id.content);

    Button button = new Button(this);
    button.setText("Your text here");
    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(300,300);
    params.width = 300;
    params.height = 300;

    button.setLayoutParams(params);
    frameLayout.addView(button);
根据您的要求配置布局参数。
此示例代码-您可以将按钮替换为floatingactionbutton

我猜..
R.id.framelayout
不在您的活动布局中。id应该在要工作的选项卡活动中。它在content\u navigation.xml中,我为每个片段使用了选项卡布局。是否也要粘贴该代码?为您的用户发布代码
content\u navigation.xml
您在viewpager页面中使用过它吗?没有,我没有在viewpager中使用它。。
 FrameLayout frameLayout = (FrameLayout)findViewById(android.R.id.content);

    Button button = new Button(this);
    button.setText("Your text here");
    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(300,300);
    params.width = 300;
    params.height = 300;

    button.setLayoutParams(params);
    frameLayout.addView(button);