java在Android布局调用中使用变量
我想对Android应用程序中已有的布局资源做一个简单的调用,如下所示。我所需要做的就是能够使用一个变量,比如myInteger(其值为1,2,3)并调用R.layout。(“fragment_main”+myInteger)而不是调用R.layout。片段2。为了明确起见,我需要Android将“fragment_main”+myInteger识别为fragment_main2,从而允许我使用变量
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main2, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
# 1 楼答案
使用
getIdentifier
获取布局id,布局文件名如下: