所以我使用的是Android Studio提供的默认导航抽屉模板的修改版本。工具栏左上方有一个图标,用于打开导航抽屉,它可以在大多数片段中使用。但是,当我移动到我的活动中的一个片段时,图标不起作用。这是具有滚动视图的少数片段之一,我已经看到删除滚动视图允许按钮再次工作。
有没有人知道如何在让按钮工作的同时保持滚动视图?
问题片段的布局文件(省略上下文名称):
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:paddingTop="@dimen/topmargin"
tools:context="com.example....FAQFragment">
<LinearLayout
android:id="@+id/faqLayout"
android:paddingLeft="@dimen/textSideMargin"
android:paddingStart="@dimen/textSideMargin"
android:paddingEnd="@dimen/textSideMargin"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
main.xml中:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/navbar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/sidebarcontent" />
</android.support.v4.widget.DrawerLayout>
片段代码:
public class FAQFragment extends Fragment {
private ArrayList<String> questions;
private ArrayList<String> answers;
private int currentlyVisible = -1;
public FAQFragment() {
// Required empty public constructor
}
public static FAQFragment newInstance() {
FAQFragment fragment = new FAQFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//todo: figure out why the scroll view is causing the tool bar to not work
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(getString(R.string.faqHeader));
View view = inflater.inflate(R.layout.fragment_faq, container, false);
LinearLayout mainView = (LinearLayout) view.findViewById(R.id.faqLayout);
readQuestionFile();
for(int i = 0; i < questions.size(); i++) {
TextView tv = new TextView(getActivity());
tv.setTag(i);
tv.setText(questions.get(i));
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.bottomMargin = 5;
tv.setLayoutParams(params);
tv.setTextSize(16);
tv.setTypeface(Typeface.DEFAULT_BOLD);
tv.setTextColor(ContextCompat.getColor(getActivity(), R.color.mainColor));
tv.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
clicked(view);
}
});
mainView.addView(tv);
TextView tv2 = new TextView(getActivity());
tv2.setTag("answer");
tv2.setText(answers.get(i));
tv2.setLayoutParams(params);
tv2.setTextSize(16);
tv2.setVisibility(View.GONE);
mainView.addView(tv2);
}
TextView copyright = new TextView(getActivity());
copyright.setTextSize(14);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
copyright.setGravity(Gravity.CENTER_HORIZONTAL);
copyright.setLayoutParams(params);
copyright.setTypeface(Typeface.DEFAULT_BOLD);
copyright.setTextColor(ContextCompat.getColor(getActivity(), R.color.mainColor));
copyright.setText("\n" + getString(R.string.copyright));
mainView.addView(copyright);
//todo: all the numbers here (text size) should refer to dimens.xml
return view;
}
片段交易:
private void changeFrag(Fragment f) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
currentFrag = f;
fragmentTransaction.replace(R.id.content_frame, currentFrag);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
谢谢!