如何在Tab布局中的RelativeLayout上显示Fab

时间:2018-01-20 08:12:43

标签: java android material-design material

我有一个标签布局,显示其中一个标签上的相对布局,我希望此特定标签显示底部的工厂。当我将fab添加到布局设计视图时,它显示但是当我在设备上测试时,fab“消失” - 根本不显示“

这是我的标签项相对布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/compose_query_content_linear_layout"
  android:background="@color/colorPrimaryDark"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

<EditText
    android:id="@+id/query_subject_editText"
    android:hint="@string/query_subject_text"
    android:textColor="@android:color/white"
    android:textColorHint="@android:color/white"
    android:padding="16dp"
    android:ems="10"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<EditText
    android:id="@+id/query_content_editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/query_subject_editText"
    android:ems="10"
    android:hint="@string/compose_query_content_text"
    android:padding="16dp"
    android:textColor="@android:color/white"
    android:textColorHint="@android:color/white" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/query_floatingActionButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_gravity="end|bottom"
    android:layout_marginBottom="91dp"
    android:layout_marginEnd="58dp"
    android:src="@drawable/ic_action_notifications_busket"
    android:tint="@android:color/white"
    app:borderWidth="4dp"
    app:fabSize="normal"
    app:rippleColor="@color/muzima_theme_blue" />


  </RelativeLayout>

这是我的标签父布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:id="@+id/message_basket_main_activity_layout"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="vertical">

<android.support.v7.widget.Toolbar
    android:id="@+id/message_basket_main_activity_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimaryDark"
    android:title="@string/app_name"
    app:theme="@style/ToolbarTheme" />

<android.support.design.widget.TabLayout 
    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:id="@+id/message_basket_tab_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget"
    android:background="@color/colorPrimaryDark"
    app:tabGravity="center"
    app:tabMaxWidth="0dp"
    app:tabMode="scrollable"
    app:tabSelectedTextColor="@color/muzima_theme_blue"
    app:tabTextColor="@android:color/white"
    tools:context="phr.muzima.org.phr.MessageBasketMainActivity">

</android.support.design.widget.TabLayout>


<android.support.v4.view.ViewPager
    android:id="@+id/message_basket_view_pager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

这里是我的片段activity.java

package phr.muzima.org.phr.fragments;

import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

import java.util.Arrays;
import java.util.List;

import phr.muzima.org.phr.R;
import phr.muzima.org.phr.adapters.UpdatesAdapter;
import phr.muzima.org.phr.model.Update;

 /**
  * Created by HP on 1/19/2018.
 */

 public class PatientQueriesFragment extends Fragment {

  FloatingActionButton fab;

public PatientQueriesFragment() {

}

  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
  container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.patient_queries_fragment_layout, 
     container, false);
    fab = view.findViewById(R.id.query_floatingActionButton);

    return view;
     }
  }

1 个答案:

答案 0 :(得分:2)

我自己也有类似的问题,发现使用协调器布局对FAB来说效果很好。将FAB放在此布局上,然后像往常一样放置相对布局。