如果您不知道这些课程会发生什么,请查看here!此外,我不是100%确定这会工作,我正在测试这个
我目前正致力于创建一个简化的基类,简化在类AttributeSet
中使用自定义xml属性
基本上,这就是我想要的最终结果 ...
public class SimpleViewImplementation extends SimpleView<LinearLayout> {
// List of members here
private String value;
public SimpleViewImplementation(Context context) {
super(context);
}
public SimpleViewImplementation(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void setFromStyledAttributes(TypedArray attr) {
// Set conditions for each member here via TypedArray (use setters)
setValue(attr.getString(R.styleable.SimpleViewImplementation_value));
}
@Override
protected void initView() {
// Set initial conditions for each member here
this.value = "this is the default value!";
}
// Getters & Setters here for members
public String getValue() { return this.value; }
public void setValue(String value) {
this.value = value;
this.updateViewOnSet();
}
}
这是完成所有魔术的“基础”类。问题实际上是班级“签名”。我需要它来扩展类型T
。 我在网上的研究中错过了怎么做,或者无法做到。如果无法完成,那么他们是否有任何建议可以获得我上面的结果。如果你不知道这些课程会有什么,那么请看here!
public abstract class SimpleView<T> { // I would like this class to extend Type T. ie SimpleView<LinearLayout> would extend this class to be a LinearLayout...getting rid of compile-time errors below
// ^ can I put anything here????
public SimpleView(Context context) {
super(context); // CTE (Compile-time error)
initView();
}
public SimpleView(Context context, AttributeSet attrs) {
super(context, attrs); // CTE
initView();
TypedArray attr = context.getTheme().obtainStyledAttributes(attrs, R.styleable.DrawerSongDetail, 0, 0);
try {
this.setFromStyledAttributes(attr);
} finally {
attr.recycle();
}
}
// Sets all members based on AttributeSet parameter
abstract protected void setFromStyledAttributes(TypedArray attr);
// Sets all initial values of members
abstract protected void initView();
private void updateViewOnSet() {
this.requestLayout(); // CTE
this.invalidate(); // CTE
}
}
答案 0 :(得分:0)
以下是如何完成的示例:http://www.lukehorvat.com/blog/android-seekbardialogpreference/
修改强>
在这样的设置中会丢失吗?
public abstract class SimpleView {
private View view;
public SimpleView(Context context, View view) {
if (view == null || !(view instanceof View)) {
throw new RuntimeException("SimpleView expects an instance of View");
}
this.view = view;
initView();
}
public SimpleView(Context context, View view, AttributeSet attrs) {
if (view == null || !(view instanceof View)) {
throw new RuntimeException("SimpleView expects an instance of View");
}
this.view = view;
initView();
TypedArray attr = context.getTheme().obtainStyledAttributes(attrs, R.styleable.DrawerSongDetail, 0, 0);
try {
this.setFromStyledAttributes(attr);
} finally {
attr.recycle();
}
}
// Sets all members based on AttributeSet parameter
abstract protected void setFromStyledAttributes(TypedArray attr);
// Sets all initial values of members
abstract protected void initView();
protected void updateViewOnSet() {
view.requestLayout();
view.invalidate();
}
}
我认为你可以用它来:
public class SimpleViewImplementation extends SimpleView {
// List of members here
private String value;
public SimpleViewImplementation(Context context) {
super(context, new RelativeLayout(context));
// Define your View here (demonstrated with RelativeLayout)
}
public SimpleViewImplementation(Context context, View view) {
// Predefined view
super(context, view);
}
public SimpleViewImplementation(Context context, AttributeSet attrs) {
super(context, new LinearLayout(context), attrs);
// Define your View here (demonstrated with LinearLayout)
}
public SimpleViewImplementation(Context context, View view, AttributeSet attrs) {
super(context, view, attrs);
// Predefined view
}
@Override
protected void setFromStyledAttributes(TypedArray attr) {
// Set conditions for each member here via TypedArray (use setters)
setValue(attr.getString(R.styleable.SimpleViewImplementation_value));
}
@Override
protected void initView() {
// Set initial conditions for each member here
this.value = "this is the default value!";
}
// Getters & Setters here for members
public String getValue() { return this.value; }
public void setValue(String value) {
this.value = value;
this.updateViewOnSet();
}
}