【问题标题】:How to add imageview to fragment?如何将图像视图添加到片段?
【发布时间】:2016-06-15 00:55:02
【问题描述】:

这样的问题有很多,但它们都解决了在返回根布局之前在onCreateView() 中添加一个视图。我想在代码执行的中间添加一个视图,在onClick()


注意这是一个片段,这就是为什么没有onCreateView()我无法更新 UI:

public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button:

            //RelativeLayout Setup
            RelativeLayout relativeLayout = new RelativeLayout(getActivity());

            relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.MATCH_PARENT));

            //ImageView Setup
            ImageView imageView = new ImageView(getActivity());

            //setting image resource
            imageView.setImageResource(R.drawable.lit);

            //setting image position
            imageView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT));

            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.BELOW, R.id.button);

            imageView.setLayoutParams(params);
            //adding view to layout
            relativeLayout.addView(imageView);


            break;
    }
}

在这里我得到一个布局实例并对其进行修改。但是,我无法将此修改后的片段布局应用回应用程序 UI。 Fragment UI修改后如何更新应用界面?

感谢您的宝贵时间。

【问题讨论】:

  • FragmentView 与其他View 一样。无论您动态创建什么Views,只需将其添加到屏幕上的ViewGroup
  • @MikeM。谢谢迈克,但即使我保存ViewGroup container 的实例并调用container.addView(relativeLayout),它也不起作用。我该怎么办?谢谢!
  • 如果您的意思是将container 传递给onCreateView(),您可能不想使用它。那是Activity 中的ViewGroup,它包含你的Fragment 的整个View。你会想要使用 ViewGroup - 例如,LinearLayoutRelativeLayout 等 - 这是你在 onCreateView() 中膨胀的布局。
  • @MikeM。但是我已经在我的问题的代码中这样做了,对吧?我将它添加到相对布局,这是我的片段的根......对吗?
  • 不,您正在那里创建一个全新的RelativeLayout。那不是在onCreateView() 中创建的。

标签: android android-layout android-fragments


【解决方案1】:

我会这样做:

  1. 在我的片段视图 xml 中,有一个名为 R.id.container 的 ViewGroup 来保存您的 ImageView。在onCreateView 中,保存对此容器的引用。
  2. 有一个单独的 xml,只包含 ImageView。这样,您就不必进行任何程序化布局。

然后在你的onClick,或者任何你需要添加ImageView的地方:

ImageView newView = LayoutInflater.from(getActivity).inflate(R.layout.image.xml);
mImageContainer.addView(newView);
// Ta da!

【讨论】:

    【解决方案2】:

    您刚刚创建了一个视图,但还没有添加到片段的布局中

    方法onCreateView()会返回这个片段的容器,保存这个实例(例如:ViewGroup container

    当您创建一个视图时,例如 onClick() 中的 RelativeLayout,将其添加到容器中,您的 UI 将会更新。

    container.addView(relativeLayout);

    例子:

    public class MyFragment extends Fragment {
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.example_fragment, container, false);
        }
    
        @Override
        public void onResume() {
            super.onResume();
    
            // i did this to see what see how button displays
            getView().postDelayed(new Runnable() {
                @Override
                public void run() {
                    // I create a new button and add it to the fragment's layout.
                    Button button = new Button(getActivity());
                    ((LinearLayout)getView()).addView(button);
                }
            }, 2000);
        }
    }
    

    布局example_fragment 只是一个线性布局

    【讨论】:

    • add 方法由于某种原因不存在?当我尝试将它添加到保存的容器时,我得到cannot resolve method add(android.widget.RelativeLayout)。我该怎么办?
    • 即使尝试container.addView 也不起作用。还有其他建议吗?非常感谢! :)
    • 您可以在扩充片段布局时创建一个字段。然后将其用作代码的任何地方的容器。或者调用getView()检索onCreateView()创建的视图
    • 对不起,我对你的意思有点困惑。所以我保存了ViewGroup container 的一个实例。但是为什么我需要getView()?我应该如何将视图添加到我的片段中?
    • 是的,您保存了 View 容器实例。然后将视图添加到容器中。致电container.add(view)
    猜你喜欢
    • 1970-01-01
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多