在我的片段中,我有一个 CollapsingToolbar,它是在onCreateView()方法中创建的:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) getActivity().findViewById(R.id.toolbar_layout);
AppBarLayout appBarLayout = (AppBarLayout) getActivity().findViewById(R.id.appBar);
return view;
}
由于我的标题可以在其他方法中更改,因此我尝试创建一个新方法来设置 CollapsingToolbar 的新标题,但它显示cannot resolve symbol 'collapsingToolbarLayout'. 我是这样试的:
public void setTitle(String passedTitle){
collapsingToolbarLayout.setTitle(passedTitle);
}
我必须更改什么才能从其他方法重命名标题?
编辑:
这是我使用该setTitle()方法的类。我尝试从内部类使用它,我试图尽可能地削减它:
public class CallsFragment extends Fragment {
String title;
private CollapsingToolbarLayout collapsingToolbarLayout;
public CallsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_calls, container, false);
collapsingToolbarLayout = (CollapsingToolbarLayout) getActivity().findViewById(R.id.toolbar_layout);
AppBarLayout appBarLayout = (AppBarLayout) getActivity().findViewById(R.id.appBar);
return view;
}
public void setTitle(String passedTitle){
collapsingToolbarLayout.setTitle(passedTitle);
}
public class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
//here the title string gets filled
Log.d("place", title);
setTitle(title);
}
}
}
相关分类