gpt4 book ai didi

android - 双工具栏显示在 fragment 上

转载 作者:太空狗 更新时间:2023-10-29 15:51:01 24 4
gpt4 key购买 nike

Double Toolbar is coming

大家好,

我的问题与我的应用程序在膨胀(或替换) fragment 时出现双工具栏有关。我有一个 Activity 类,其中包含抽屉导航及其带有导航工具栏图标的工具栏。

有一些 fragment 我不想显示 Activity 工具栏我只需要显示 fragment 工具栏:

但是在替换它时显示 Activity 以及 fragment 工具栏,任何对我有帮助的建议。

我已经尝试过了:

我的 fragment :

public class FoodDetailsFragment extends Fragment{
View view;
private Toolbar toolbar;
private ImageView imageView,tabBg;
private CollapsingToolbarLayout collapsingToolbar;
private TabPagerAdapter tabPagerAdapter;
private ViewPager mViewPager;
private TabLayout mTabLayout;
CoordinatorLayout.Behavior behavior;
SendMessage listener;

public interface SendMessage{
void sendData(String s);
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try{
listener= (SendMessage) activity;

}catch (ClassCastException exp){
throw new ClassCastException("Class Cast Exp");
}


}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view=inflater.inflate(R.layout.food_details,container,false);
setHasOptionsMenu(true);
listener.sendData("ds");

imageView= (ImageView) view.findViewById(R.id.backdrop);
tabBg= (ImageView) view.findViewById(R.id.tabBg);
collapsingToolbar=(CollapsingToolbarLayout) view.findViewById(R.id.collapsing_toolbar);

setToolbar(view);
setImage();

mViewPager= (ViewPager) view.findViewById(R.id.viewpager);
mTabLayout= (TabLayout) view.findViewById(R.id.detail_tabs);
tabPagerAdapter=new TabPagerAdapter(getActivity().getSupportFragmentManager());
mViewPager.setAdapter(tabPagerAdapter);
mTabLayout.setTabsFromPagerAdapter(tabPagerAdapter);
mTabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
mTabLayout.setupWithViewPager(mViewPager);

mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
if(position==0){
Glide.with(getActivity()).load(R.drawable.onet).into(imageView);
Glide.with(getActivity()).load(R.drawable.oneb).into(tabBg);
} else {
Glide.with(getActivity()).load(R.drawable.twot).into(imageView);
Glide.with(getActivity()).load(R.drawable.twob).into(tabBg);
}
imageView.invalidate();
}

@Override
public void onPageScrollStateChanged(int state) {

}
});
return view;


}
private void setToolbar(View view) {
toolbar= (Toolbar) view.findViewById(R.id.toolbarone);

((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);

((AppCompatActivity) getActivity()).getSupportActionBar();
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(true);




if (toolbar != null) {
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
}


}
private void setImage() {
Glide.with(this).load(R.drawable.resfor).into(imageView);
Glide.with(getActivity()).load(R.drawable.resone).into(tabBg);
}
class TabPagerAdapter extends FragmentStatePagerAdapter {

public TabPagerAdapter(FragmentManager fm){
super(fm);
}
@Override
public Fragment getItem(int position) {

switch (position){
case 0:
FoodDetailedInfoFragment tab1 = new FoodDetailedInfoFragment();
return tab1;
case 1:
FoodReviewFragment tab2 = new FoodReviewFragment();
return tab2;

default:
return null;
}




}

@Override
public int getCount() {
return 2;
}

@Override
public CharSequence getPageTitle(int position) {
if(position==0){
return "INFO";
}else if(position==1){
return "REVIEW";
}


return "INFO" ;
}
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.my_custom_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}

}

fragment 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"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="256dp"

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"

app:layout_scrollFlags="scroll|exitUntilCollapsed">

<ImageView

android:id="@+id/backdrop"

android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="fitXY"
app:layout_collapseMode="none" />

<android.support.v7.widget.Toolbar
android:id="@+id/toolbarone"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">


</android.support.v7.widget.Toolbar>


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

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

<FrameLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="bottom"

app:layout_anchor="@+id/appbar"
app:layout_anchorGravity="bottom"
app:layout_collapseMode="none">

<ImageView
android:id="@+id/tabBg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:scaleType="fitXY" />

<android.support.design.widget.TabLayout
android:id="@+id/detail_tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:tabMaxWidth="0dp"
android:layout_gravity="bottom"
android:background="#00000000"
app:layout_anchor="@+id/appbar"
app:layout_anchorGravity="bottom"
app:layout_collapseMode="none"
app:tabGravity="fill"



app:tabIndicatorColor="#fff"
app:tabIndicatorHeight="2dp"
app:tabMode="scrollable"
app:tabSelectedTextColor="#fff"
app:tabTextColor="#fff" />

</FrameLayout>


<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

启动 Activity xml:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.CoordinatorLayout
android:id="@+id/container_main"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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:elevation="4dp"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

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




<FrameLayout
android:layout_below="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="false"
android:focusableInTouchMode="false"
android:background="@color/faintwhite"
android:id="@+id/container">

</FrameLayout>





</RelativeLayout>

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/spacing"
android:src="@drawable/ic_share_grey_600_24dp"
app:backgroundTint="@color/colorAccent" />

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


<!-- Navigation Drawer-->
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#000000">

</android.support.v7.widget.RecyclerView>

</android.support.v4.widget.DrawerLayout>






<!--
<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="7dp">


<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="false"
android:focusableInTouchMode="false"
android:background="@color/faintwhite"
android:id="@+id/container">
<include
android:id="@+id/tool_bar"
layout="@layout/app_bar"/>
&lt;!&ndash;<RelativeLayout

android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include
android:id="@+id/tool_bar"
layout="@layout/app_bar"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"

android:layout_below="@+id/tool_bar">
<LinearLayout
android:id="@+id/lin_lay"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"


/>
</LinearLayout>

<LinearLayout
android:id="@+id/linear2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

/>
</LinearLayout>
</LinearLayout>

</RelativeLayout>&ndash;&gt;

</FrameLayout>

<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/white"
app:headerLayout="@layout/drawer_header"
app:itemIconTint="@drawable/drawer_icon_selector"
app:itemTextColor="@drawable/drawer_text_selector"
app:menu="@menu/drawer" />

&lt;!&ndash;<android.support.v7.widget.RecyclerView
android:id="@+id/RecyclerView"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="left"

android:background="#ffffff"
android:scrollbars="vertical">

</android.support.v7.widget.RecyclerView>&ndash;&gt;
</android.support.v4.widget.DrawerLayout>-->

最佳答案

使用主题

<style name="AppCompat" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>

</style>

关于android - 双工具栏显示在 fragment 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39163374/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com