在我的應用程序中,我想在選項卡的佈局中設置FloatingActionButton。如何設置FloatingActionButton在選項卡布局中的屏幕上?
在項目中,我首先創建和TabLayout和ViewPager去年FloatingActionButton像圖像
visit_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>
Visit_tab.java
package com.Weal.sachin.omcom;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class VisitTab extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 3 ;
android.support.v4.app.FragmentManager mFragmentManager;
View view;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getActivity().setTitle("Visit");
GetCurrentGPSLocation gps = new GetCurrentGPSLocation(getActivity());
if (gps.canGetLocation()) {
// \n is for new line
}else {
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
View x = inflater.inflate(com.Weal.sachin.omcom.R.layout.activity_visit_tab,null);
tabLayout = (TabLayout) x.findViewById(com.Weal.sachin.omcom.R.id.sliding_tabs_visit);
viewPager = (ViewPager) x.findViewById(com.Weal.sachin.omcom.R.id.viewpager_visit);
viewPager. setOffscreenPageLimit(3);
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
FloatingActionButton fab = (FloatingActionButton)x.findViewById(R.id.fab111);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentTransaction t = getFragmentManager().beginTransaction();
Add_Visit mFrag = new Add_Visit();
t.replace(R.id.framelayout, mFrag);
t.commit();
}
});
tabLayout.setupWithViewPager(viewPager);
return x;
}
class MyAdapter extends FragmentPagerAdapter {
com.Weal.sachin.omcom.TodayVisit TodayVisit;
Home home;
UpdatesFragment UpdatesFragment;
public MyAdapter(FragmentManager fm) {
super(fm);
}
/**
* Return fragment with respect to Position .
*/
@Override
public Fragment getItem(int position)
{
switch (position){
case 0 : return new TodayVisit();
case 1 : return new YesterdayVisit();
case 2 : return new AllVisit();
}
return null;
}
@Override
public int getCount() {
return int_items;
}
/**
* This method returns the title of the tab according to the position.
*/
@Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0 :
return "Today";
case 1 :
return "Yesterday";
case 2 :
return "Visit All";
}
return null;
}
}
}
在這個XML文件floatinActionButton顯示完美,但它取代了ViewPager,但我想設置floatinActionButton在ViewPager上。
我猜...'R.id.framelayout'不在您的活動佈局中。 – rafsanahmad007
該ID應該在您的選項卡活動中起作用 – rafsanahmad007
它位於content_navigation.xml中,並且我爲每個片段使用了Tab Layout我也應該粘貼該代碼? – Andie