将 "this" 作为根传递给自定义组件中的 LayoutInflater.inflate()
Passing "this" as root to LayoutInflater.inflate() in a custom component
我正在为扩展 LinearLayout 的自定义组件编写代码。它将在顶部包括一个微调器,以及下面的一些设置,具体取决于微调器的设置。即,当用户 selects "Apple" 在微调器上时,会出现 "color" 选项,当他们 select "Banana" 时会出现 "length" 选项.
由于微调器选项可能有许多与之关联的设置,因此我在布局 XML 中定义每组设置,并将 "merge" 作为根标记。然后我在每个构造函数中调用 initViews() 来膨胀视图,这样我可以稍后 add/remove 它们。
这是 class 的代码:
public class SchedulePickerView extends LinearLayout {
protected Context context;
protected Spinner typeSpinner;
protected ViewGroup defaultSetters; // ViewGroup to show when no schedule is selected in the spinner
protected ViewGroup simpleSetters; // ViewGroup to show when SimpleSchedule is selected in the spinner
public SchedulePickerView(Context context) {
super(context);
this.context = context;
initViews();
}
public SchedulePickerView(Context context, AttributeSet attr) {
super(context, attr);
this.context = context;
initViews();
}
public SchedulePickerView(Context context, AttributeSet attr, int defstyle) {
super(context, attr, defstyle);
this.context = context;
initViews();
}
private void initViews() {
// Init typeSpinner
typeSpinner = (Spinner) findViewById(R.id.schedulepickerSpinner);
// Init setters (ViewGroups that show settings for the various types of schedules
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// ERROR IS ON THIS LINE:
defaultSetters = inflater.inflate(R.layout.container_schedulesetter_default, this);
}
}
我在标记的行上收到此错误:"Incompatible types: Required = ViewGroup, Found = View"。但是根据 this 文档,LinearLayout 扩展了 ViewGroup。我什至尝试过将 "this" 转换为 ViewGroup,但奇怪的是 IDE 将转换变灰(因为很明显,每个 LinearLayout 已经是一个 ViewGroup)。那么为什么会出现问题?
inflate()
return 是一个 View
并且您正在尝试将其分配给更具体的 ViewGroup
变量。这不是 this
作为父视图的问题 - 您需要对 return 值进行强制转换:
defaultSetters = (ViewGroup)inflater.inflate(...)
我正在为扩展 LinearLayout 的自定义组件编写代码。它将在顶部包括一个微调器,以及下面的一些设置,具体取决于微调器的设置。即,当用户 selects "Apple" 在微调器上时,会出现 "color" 选项,当他们 select "Banana" 时会出现 "length" 选项.
由于微调器选项可能有许多与之关联的设置,因此我在布局 XML 中定义每组设置,并将 "merge" 作为根标记。然后我在每个构造函数中调用 initViews() 来膨胀视图,这样我可以稍后 add/remove 它们。
这是 class 的代码:
public class SchedulePickerView extends LinearLayout {
protected Context context;
protected Spinner typeSpinner;
protected ViewGroup defaultSetters; // ViewGroup to show when no schedule is selected in the spinner
protected ViewGroup simpleSetters; // ViewGroup to show when SimpleSchedule is selected in the spinner
public SchedulePickerView(Context context) {
super(context);
this.context = context;
initViews();
}
public SchedulePickerView(Context context, AttributeSet attr) {
super(context, attr);
this.context = context;
initViews();
}
public SchedulePickerView(Context context, AttributeSet attr, int defstyle) {
super(context, attr, defstyle);
this.context = context;
initViews();
}
private void initViews() {
// Init typeSpinner
typeSpinner = (Spinner) findViewById(R.id.schedulepickerSpinner);
// Init setters (ViewGroups that show settings for the various types of schedules
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// ERROR IS ON THIS LINE:
defaultSetters = inflater.inflate(R.layout.container_schedulesetter_default, this);
}
}
我在标记的行上收到此错误:"Incompatible types: Required = ViewGroup, Found = View"。但是根据 this 文档,LinearLayout 扩展了 ViewGroup。我什至尝试过将 "this" 转换为 ViewGroup,但奇怪的是 IDE 将转换变灰(因为很明显,每个 LinearLayout 已经是一个 ViewGroup)。那么为什么会出现问题?
inflate()
return 是一个 View
并且您正在尝试将其分配给更具体的 ViewGroup
变量。这不是 this
作为父视图的问题 - 您需要对 return 值进行强制转换:
defaultSetters = (ViewGroup)inflater.inflate(...)