我必须自定义XML视图。一个叫做按钮,另一个叫做main_alt。我想要实现的是当勾选共享pref时它会改变布局。
当用户首次启动应用程序时,我希望它显示buttons.xml 在添加更改XML的设置之前,我是如何做到这一点的,就是在mainActivity.xml中添加标记
我的共享首页很简单且有效
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if let index = tabBarController.viewControllers?.index(of: viewController),
index < tabBarTypes.count {
let type = tabBarTypes[index]
switch type {
case .menu:
// perform your menu presenting here
return false
case .controller:
// do nothing, just return true, tab bar will do all work for you
return true
}
}
return true
}
我使用
在我的主要活动中调用该方法cb6.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (cb6.isChecked()) {
editor.putBoolean("alt_layout",true);
editor.putBoolean("checkbo6state", true);
editor.commit();
Log.i("Alt_Layout: ", "Activated");
}else{
editor.putBoolean("alt_layout",false);
editor.putBoolean("checkbox6state", false);
editor.commit();
Log.i("Alt_Layout: ","Deactivated");
}
}
});
但我在logcat
中收到此错误sharedpreferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean alt_Layout = sharedpreferences.getBoolean("alt_layout",false);
if (alt_Layout== true){
View view1;
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
view1 = inflater.inflate(R.layout.main_alt,null);
LinearLayout comp = (LinearLayout) findViewById(R.id.main);
view1.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
<--This is line 80-->
comp.addView(view1);
} else {
View view;
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.buttons,null);
LinearLayout comp = (LinearLayout) findViewById(R.id.main);
view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
comp.addView(view);
}
任何帮助总是非常感谢伙计们。感谢
答案 0 :(得分:2)
好的,你需要这样做:
LinearLayout comp = (LinearLayout) findViewById(R.id.main);
if (alt_Layout) {
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.id.main, comp, true);
} else {
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.buttons, comp, true);
}