示例代码:
public class CustomImageVIew extends LinearLayout {
private Image mImage = null;
private ImageView mImageView;
private ProgressBar mProgressBar;
private RelativeLayout mLayout;
public static enum State {NOT_LOADED, IMAGE_LOADING, IMAGE_LOADED};
private State state;
public CustomImageVIew(Context context) {
this(context, null);
}
public CustomImageVIew(Context context, AttributeSet attrs) {
super(context, attrs);
View v = View.inflate(getContext(), R.layout.gallery_image_view, null);
mLayout = (RelativeLayout) v;
addView(mLayout);
mImageView = (ImageView) mLayout.findViewById(R.id.gallery_image_view_image);
mProgressBar = (ProgressBar) mLayout.findViewById(R.id.gallery_image_view_progress_bar);
setState(State.NOT_LOADED);
}
private void setState(State newState) {
if (state != newState) {
state = newState;
switch (state) {
case NOT_LOADED:
mImageView.setVisibility(View.GONE);
mProgressBar.setVisibility(View.GONE);
break;
case IMAGE_LOADING:
mImageView.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.VISIBLE);
break;
case IMAGE_LOADED:
mImageView.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.GONE);
break;
}
}
}
public State getState() {
return state;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/gallery_image_view_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:id="@+id/gallery_image_view_progress_bar"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:clickable="false"/>
</RelativeLayout>
当我在onCreate方法之外调用setState(State.IMAGE_LOADED)时,没有任何反应。 它有什么问题?
答案 0 :(得分:0)
我认为你真的不需要自定义视图(java文件)。您可以使用在活动的xml和LinearLayout
方法中声明的onCreate()
来执行此操作。 XML使您的生活更轻松。您应该在编码时遵循Android的最佳做法。