我在主xml文件中有以下片段:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_activity_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/prayer_times_background"
tools:context="com.yceo.namazvakti.MainActivity">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/main_menu"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">
</FrameLayout>
<ImageView
android:id="@+id/settingButton"
android:layout_width="@dimen/top_right_Button_height_width_size"
android:layout_height="@dimen/top_right_Button_height_width_size"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:padding="@dimen/top_right_button_pedding"
android:layout_marginTop="@dimen/top_right_Button_margin_top"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:src="@drawable/ic_menu_settings"
android:scaleType="fitCenter"/>
<fragment
android:id="@+id/main_menu"
android:name="com.yceo.namazvakti.fragments.MenuFragment"
android:layout_width="match_parent"
android:layout_height="@dimen/menu_layout_height"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
tools:layout="@layout/fragment_menu">
</fragment>
</RelativeLayout>
我试图隐藏@+id/main_menu
,但我无法隐藏。我尝试了以下选项,但它们不起作用:
使用FragmentManager
获取片段本身,并将其隐藏FragmentManager
FragmentManager fragmentManager = getSupportFragmentManager();
MenuFragment menuFragment = (MenuFragment)fragmentManager.findFragmentById(R.id.main_menu);
fragmentManager.beginTransaction().hide(menuFragment).commit();
获取片段View
并设置其可见性GONE
View activityRootView = findViewById(R.id.main_activity_layout);
View fragmentMenu = activityRootView.findViewById(R.id.main_menu);
fragmentMenu.setVisibility(View.GONE);
任何人都可以帮助我吗?
更新
更明确。我的MainActivity中有以下metod
public void setMenuVisibility(int visibility){
Log.d(LOG_TAG, "MainActivity - setMenuVisibility");
switch (visibility) {
case View.VISIBLE:
Log.d(LOG_TAG, "MainActivity - setMenuVisibility - Showing menuFragment...");
break;
case View.GONE:
Log.d(LOG_TAG, "MainActivity - setMenuVisibility - Hiding menuFragment...");
break;
}
fragmentMenu.setVisibility(View.GONE);
if (menuFragment != null) {
View v = menuFragment.getView();
if (v != null)
v.setVisibility(visibility);
}
fragmentManager.beginTransaction().hide(menuFragment).commit();
}
我从片段中称这个方法为:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mainActivity = (MainActivity)getActivity();
mainActivity.setSettingsButtonVisibility(View.GONE); <-- This works
mainActivity.setMenuVisibility(View.GONE); // <--- But this is not
View rootView = inflater.inflate(R.layout.fragment_quran_page, container, false);
backImageView = (ImageView)rootView.findViewById(R.id.back);
backImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fm = getActivity().getSupportFragmentManager();
fm.popBackStack();
}
});
return rootView;
}
答案 0 :(得分:0)
它已在这里解决:检查以下链接,希望它可以帮助:
Show/Hide Fragments and change the visibility attribute programmatically
答案 1 :(得分:0)
hide
才有效。它不会影响xml
Fragment
答案 2 :(得分:0)
当我想隐藏它并恢复它的值以再次显示时,我已将片段布局高度设置为0。
public void setMenuVisibility(int visibility){
Log.d(LOG_TAG, "MainActivity - setMenuVisibility");
ViewGroup.LayoutParams lp;
switch (visibility) {
case View.VISIBLE:
Log.d(LOG_TAG, "MainActivity - setMenuVisibility - Showing menuFragment...");
lp = fragmentMenu.getLayoutParams();
lp.height = oldLp.height;
fragmentMenu.setLayoutParams(lp);
break;
case View.GONE:
Log.d(LOG_TAG, "MainActivity - setMenuVisibility - Hiding menuFragment...");
lp = fragmentMenu.getLayoutParams();
oldLp = new ViewGroup.LayoutParams(lp.width, lp.height);
lp.height = 0;
fragmentMenu.setLayoutParams(lp);
break;
}
}