Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android可绘制图像未显示_Android_Android Drawable - Fatal编程技术网

Android可绘制图像未显示

Android可绘制图像未显示,android,android-drawable,Android,Android Drawable,我正在尝试用我的可绘制hdpi文件夹中的图像编程设置我的应用程序的背景。它只在R.drawable中看到默认的ic_启动程序文件,所以当我去设置背景时,它根本不存在。我使用这个类作为带有滑动菜单的片段类,所以这可能与它有关 编辑:我可以在xml文件中设置背景,但无法在oncreateview方法中设置背景 public class inputClass extends Fragment { @Override public View onCreateView(LayoutInflater inf

我正在尝试用我的可绘制hdpi文件夹中的图像编程设置我的应用程序的背景。它只在R.drawable中看到默认的ic_启动程序文件,所以当我去设置背景时,它根本不存在。我使用这个类作为带有滑动菜单的片段类,所以这可能与它有关

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

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;
}

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

  • 您应该将图像文件放在所有可绘制目录中。我的意思是,您应该将该图像放入
    可绘制ldpi
    可绘制mdpi
    等等。此外,您还应该注意图像的大小,因为每个文件夹对应于屏幕大小。请看看这个

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

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


    希望这会有所帮助。

    如果您从
    activity
    扩展您的活动,然后使用
    android:src
    而不是
    app:srcCompat


    这在我的例子中解决了一个问题。

    但是如果您的drawable是一个
    VectorDrawable