是否可以在自定义View类中创建另一个控件。
我想在自定义类中添加图像和进度条。
View类中没有addView方法。
ViewGroup是唯一的选择吗?
public class CustomClass extends FrameLayout {
Context c;
ProgressBar progressBar;
ImageView imageView;
public CustomClass(Context context) {
super(context);
loadControls(context);
}
public CustomClass(Context context, AttributeSet attrs) {
super(context, attrs);
loadControls(context);
}
public CustomClass(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
loadControls(context);
}
public void loadControls(Context context)
{
c = context;
progressBar = new ProgressBar(c, null, android.R.attr.progressBarStyleInverse);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT, Gravity.TOP | Gravity.LEFT);
imageView = new ImageView(c);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
//imageView.setBackgroundColor(color.darker_gray);
addView(imageView, params);
params = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
addView(progressBar, params);
progressBar.setVisibility(View.VISIBLE);
}}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句