我有两个问题/问题:)
首先:我实现了Sliding Tabs Layout。现在片段用于Tabs在底部被截断: Fragment gets cut off (FAB is on bottom|right)
第二:当我按下FAB时,我想打开一个新的活动(FAB在片段上)。但是当我按下它时,会打开一个空白的活动(实际活动有一个布局并且正在自行运行),当我按下后退按钮时,我退出应用程序。
对于第一个问题:这是我在MainActivity的onCreate中调用的Methode(其中实现了Sliding Tabs Layout)(我不知道你是否需要ViewPager的代码所以我没有包含它,如果你需要请告诉我):
private void excTasks(){
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setOffscreenPageLimit(4);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
这是ViewPagerAdapter:
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
//return mFragmentTitleList.get(position);
// Wenn man nur Icons will -> return null, sont das obrige verwenden
return null;
}
}
在这里我添加片段:
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new NewsFragment(), "NEWS");
adapter.addFragment(new ProfileFragment(), "PROFILE");
adapter.addFragment(new NotificationFragment(), "NOTIF");
adapter.addFragment(new AboutFragment(), "ABOUT");
viewPager.setAdapter(adapter);
}
这是MainActivity的.xml:
<android.support.design.widget.CoordinatorLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"
app:tabIndicatorColor="@color/white"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
关于第二个问题: 这是我想要FAB的片段(在onViewCreated中)(按下按钮时应该打开的是OfferActivity):
//FAB
FloatingActionButton fab = (FloatingActionButton) v.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Click action
Intent intent = new Intent(con, OfferActivity.class);
getActivity().startActivity(intent);
}
});
如前所述,当在一个标签中显示时,OfferActivity工作正常,因此我不会包含它的代码。 这是FAB xml(在RelativeLayout中):EDIT:包含整个文件
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@android:id/list"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:layout_weight="0"
android:layout_marginTop="5dp"
android:foregroundGravity="fill"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/fab_margin"
android:layout_marginRight="@dimen/fab_margin"
android:src="@drawable/fab_add"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:baselineAlignBottom="true"
android:clickable="true"
app:backgroundTint="@color/colorPrimary" />
感谢您的回答,对不起,如果这个问题已经问过了! :)
编辑:这是styles.xml,我已经包含了整个FAB xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:actionBarDivider">@color/colorPrimary</item>
<!--<item name="android:windowTranslucentStatus" tools:targetApi="kitkat">true</item>-->
</style>
<style name="MyMaterialTheme" parent="AppTheme">
<item name="android:windowContentTransitions" tools:targetApi="lollipop">true</item>
<item name="android:windowAllowEnterTransitionOverlap" tools:targetApi="lollipop">true</item>
<item name="android:windowAllowReturnTransitionOverlap" tools:targetApi="lollipop">true</item>
<item name="android:windowSharedElementEnterTransition" tools:targetApi="lollipop">@android:transition/move</item>
<item name="android:windowSharedElementExitTransition" tools:targetApi="lollipop">@android:transition/move</item>
</style>
答案 0 :(得分:1)
我认为使用layout_baselineAlignBottom
属性会产生问题。 app:layout_anchor
属性使我们能够指定我们希望锚定浮动操作按钮的布局。 app:layout_anchorGravity
指定锚点的位置。将以下浮动操作按钮xml放在</android.support.design.widget.CoordinatorLayout>
之前的末尾。
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/fab_margin"
android:layout_marginRight="@dimen/fab_margin"
android:src="@drawable/fab_add"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:baselineAlignBottom="true"
android:clickable="true"
app:backgroundTint="@color/colorPrimary"
app:layout_anchor="@id/viewpager"
app:layout_anchorGravity="bottom|right|end" />
请注意:浮动操作按钮只能与协调器布局一起使用。
编辑:好的,将您的相对布局包装在协调器布局中,然后进行更改
在浮动操作按钮xml中app:layout_anchor="@id/viewpager"
到app:layout_anchor="@id/listview"
。
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:layout_weight="0"
android:layout_marginTop="5dp"
android:foregroundGravity="fill"/>
</RelativeLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:clickable="true"
app:backgroundTint="@color/colorPrimary"
app:layout_anchor="@id/list"
android:src="@drawable/fab_add"
android:layout_marginBottom="@dimen/fab_margin"
android:layout_marginRight="@dimen/fab_margin"
app:layout_anchorGravity="bottom|right|end"/>
</android.support.design.widget.CoordinatorLayout>
答案 1 :(得分:0)
您可以将FloatingActionButton
与CoordinatorLayout
一起使用,如下所示:
<CoordinatorLayout>
<AppbarLayout/>
<scrollableView/>
<FloatingActionButton/>
</CoordinatorLayout>
像这样的东西(你可以使用SupportLibrary或在CoordinatorLayout
中使用它)这是最好的方法:
<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:id="@+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabIndicatorColor="@color/white"
app:tabMode="fixed" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:src="@drawable/ic_add"
app:backgroundTint="@color/ColorAccent"
app:borderWidth="0dp"
app:fabSize="normal"
app:layout_anchor="@id/coordinator"
app:layout_anchorGravity="bottom|right|end" />
</android.support.design.widget.CoordinatorLayout>
答案 2 :(得分:0)
希望有些东西搞砸了值-v21 style.xml。
尝试在value-v21 style.xml中添加以下属性集。
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:fitsSystemWindows">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">false</item>
并将以下代码添加到fab片段。
private int getNavigationBarHeight() {
Resources resources = getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
return resources.getDimensionPixelSize(resourceId);
}
return 0;
}
@Override
public void onCreateView(Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int navBarHeight = getNavigationBarHeight();
findViewById(R.id.base_frame).setPadding(0, 0, 0, navBarHeight);
findViewById(R.id.menu_frame).setPadding(0, 0, 0, navBarHeight);
}
}
类似问题here
祝你好运..!
答案 3 :(得分:0)
试试这段代码: -
public class DeliveryTab 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("Delivery");
View x = inflater.inflate(com.Weal.sachin.omcom.R.layout.activity_delivery_tab,null);
tabLayout = (TabLayout) x.findViewById(com.Weal.sachin.omcom.R.id.sliding_tabs_delivery);
viewPager = (ViewPager) x.findViewById(com.Weal.sachin.omcom.R.id.viewpager_delivery);
viewPager. setOffscreenPageLimit(3);
FloatingActionButton fab = (FloatingActionButton)x.findViewById(R.id.fabdelivery);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentTransaction t = getFragmentManager().beginTransaction();
Add_Delivery mFrag = new Add_Delivery();
t.replace(R.id.framelayout, mFrag);
t.commit();
}
});
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
tabLayout.setupWithViewPager(viewPager);
return x;
}
class MyAdapter extends FragmentPagerAdapter {
com.Weal.sachin.omcom.TodayVisit TodayVisit;
Home home;
com.Weal.sachin.omcom.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 DeliveryToday();
case 1 : return new YesterdayDelivery();
case 2 : return new Delivery_all();
}
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 "Delivery All";
}
return null;
}
}
}
这里是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_delivery"
android:layout_width="match_parent"
android:layout_height="60dp"
app:tabTextColor="#d2cece"
app:tabSelectedTextColor="#fff"
android:background="#11977c"
/>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager_delivery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<fragment android:name="com.Weal.sachin.omcom.TabFragment"
android:id="@+id/tabFragment"
android:layout_width="match_parent"
android:layout_height="100dp" />
</android.support.v4.view.ViewPager>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fabdelivery"
android:layout_width="75dp"
android:layout_height="124dp"
android:layout_marginTop="50dp"
android:layout_gravity="bottom|end"
android:backgroundTint="#11977c"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/addplus"/>
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>