Android-RelativeLayout 子项的可见性消失的问题
这里是示例代码:
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) 时,什么也没有发生。 这可能是什么问题?
Here the sample code:
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>
When i call setState(State.IMAGE_LOADED) outside of onCreate method nothing happens.
What problem can it be ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你真的不需要自定义视图(java 文件)。您可以使用 xml 中声明的
LinearLayout
和 Activity 的onCreate()
方法来完成此操作。 XML 使您的生活变得更加轻松。您在编码时应该遵循 Android 的最佳实践。I think you really dont require a custom view (java file). You can do this using a
LinearLayout
declared in xml andonCreate()
method of your activity. XML makes your life a lot easier. You should follow Android's best practices while coding.