RecyclerView的GridLayout视图android上的背景图像 [英] Background image on the RecyclerView's GridLayout views android

查看:58
本文介绍了RecyclerView的GridLayout视图android上的背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我对SO的第一个问题.

我刚刚将RecyclerView与CardViews的GridManagerLayout一起使用.现在,这一切都可以与CustomAdapter以及其他设备完美配合,但是我想在这些CardViews后面设置一个背景图像,这样在它后面就不会那么裸露了.

现在,我尝试使用背景图像(包含RecyclerView的XML布局(FrameLayout))将其设置为Fragment,然后尝试使用RecyclerView本身进行设置.

尝试设置图像是我的职业,因为这样做似乎很棘手.

有什么办法可以做到吗?

包含RecyclerView的布局:

 < LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"xmlns:tools ="http://schemas.android.com/tools" android:layout_width ="match_parent"android:layout_height ="match_parent"android:background ="@ drawable/background"工具:context ="fragments.SpaceFlightFragment">< FrameLayoutandroid:layout_width ="match_parent"android:layout_height ="match_parent">< android.support.v7.widget.RecyclerViewandroid:id ="@ + id/recyclerView"android:layout_width ="match_parent"android:layout_height ="match_parent"/></FrameLayout> 

在片段中创建View的方法:

  @Override公共视图onCreateView(LayoutInflater充气器,ViewGroup容器,捆绑保存的实例状态){//扩充此片段的布局查看v = inflater.inflate(R.layout.fragment_spaceflight,container,false);RecyclerView recyclerView =(RecyclerView)v.findViewById(R.id.recyclerView);SpaceFlightAdapter spaceFlightAdapter =新的SpaceFlightAdapter(任务);recyclerView.setAdapter(spaceFlightAdapter);recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),2));返回v;} 

现在,我尝试将XML布局中的背景图像设置为所有标签,LinearLayout,FrameLayout和RecyclerView.

我还尝试过在OnCreateView方法中向其中动态添加图片.....还是什么也没有:/.

解决方案

找到了答案.是一个愚蠢的错误!一切都做得不错,但是图像有点大,没有被创建.因此,测试了一个较小的图像以使其获得机会,并且它起作用了.如果这是一个大问题,则期望OutOfMemory异常.不过要感谢david145和hars.

This is my first question with SO.

I've just used RecyclerView with a GridManagerLayout of CardViews. Now it all works perfectly with the CustomAdapter and all, but I want to set a background Image behind those CardViews so it's not so bare behind them.

Now I tried setting the background Image to the Fragment using it, the XML layout (FrameLayout) that holds the RecyclerView and then tried the same with the RecyclerView itself.

It's killing me to try and set an image as I'm set on doing it since it seems to be tricky.

Is there any way that it can be done?

Edit:

The layout holding the RecyclerView:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
tools:context="fragments.SpaceFlightFragment">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</FrameLayout>

The method that creates the View in the fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_spaceflight, container, false);

    RecyclerView recyclerView = (RecyclerView)v.findViewById(R.id.recyclerView);

    SpaceFlightAdapter spaceFlightAdapter = new SpaceFlightAdapter(missions);

    recyclerView.setAdapter(spaceFlightAdapter);

    recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
    return v;
}

Now I tried setting the background image in the XML layout to all the tags, LinearLayout, FrameLayout and the RecyclerView.

I've also tried to Dynamically add the image to them in the OnCreateView method.....still nothing :/.

解决方案

Found the answer. Was a silly mistake! Was doing everything right, but the image was a bit to big and didn't get created. So tested a smaller image to chance it and it worked.....should of tried another image before asking on SO. Was expecting the OutOfMemory exception if it was a big picture problem. Thanks though to david145 and hars.

这篇关于RecyclerView的GridLayout视图android上的背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆