Android-RelativeLayout 子项的可见性消失的问题

发布于 2024-12-01 11:15:24 字数 2725 浏览 0 评论 0原文

这里是示例代码:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑咖 2024-12-08 11:15:24

我认为你真的不需要自定义视图(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 and onCreate() method of your activity. XML makes your life a lot easier. You should follow Android's best practices while coding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文