Android可绘制图像未显示 [英] Android Drawable Image not Showing

查看:71
本文介绍了Android可绘制图像未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过drawable-hdpi文件夹中的图像以编程方式设置应用程序的背景.它仅在R.drawable中看到默认的ic_launcher文件,因此当我设置背景时,它根本不存在.我将此类用作带有滑动菜单的片段类,所以也许与它有关.

I am trying to programically set the background of my app with an image in my drawable-hdpi folder. It is only seeing the default ic_launcher file in R.drawable so when I go to set the background, its simply unexistant. I am using this class as a fragment class with a swipe menu so maybe that has something to do with it.

我可以在xml文件中设置背景,但无法在oncreateview方法中设置背景.

I was able to set the background in my xml file, but I couldn't set the background in my oncreateview method.

public class inputClass extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.input_xml, container, false);

    rootView.setBackgroundResource(R.drawable.hidden);


    return rootView;
}

推荐答案

问题是您的可绘制图像文件仅在drawable-hdpi文件夹中可用.为了正确设置背景,您可以执行以下两种方法.

The problem is that your drawable image file is only available drawable-hdpi folder. In order to set the background properly, you can do two following ways.

  1. 您应该将图像文件放置到所有可绘制目录中.我的意思是您应该将该图像放入drawable-ldpidrawable-mdpi等.另外,您应该注意图像的大小,因为每个文件夹都对应于屏幕大小.请查看此 API指南

  1. You should place your image file to all drawable directories. I mean you should put that image into drawable-ldpi, drawable-mdpi and so on. Also you should be careful about the size of the image because each folder corresponds to screen sizes. Please check out this API Guide

第二种方法是第一种方法.在res/文件夹中创建一个目录,并将其命名为drawable,然后将图像文件从drawable-hdpi移到drawable.

Second way is the alternative for the first one. Create a directory within res/ folder and name it as drawable and then move your image file from drawable-hdpi to drawable.

然后,清理项目并重新启动.

Then, clean the project and relaunch.

希望这会有所帮助.

这篇关于Android可绘制图像未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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