显示图像字符串资源 (xml) 安卓



如何在字符串数组中以xml格式显示图像?

     <string-array name = "cities_info"> 
    <item>
        <![CDATA[<i><b>Image1:</b> Map of Africa. 
          Cape Town (Afrikaans: Kaapstad [ˈkɑːpstɐt]; Xhosa: Ikapa) is a
          city in South Africa. It ranks third among the most populous urban areas in South Africa, after Johannesburg and Durban, and has roughly the
          same population as the Durban Metropolitan Area. It is also 
          the provincial capital and primate city of the Western Cape
        ]]>
    </item>

我尝试过使用 html 代码,但它不起作用

<img src="capetown.jpg" alt="city" style="width:300px;height:300px;">

 <img src="@drawable/capetown.jpg" alt="city" style="width:300px height:300px;">
您应该在

res/values 文件夹中的array.xml文件中使用该数组来创建其他资源的数组,例如可绘制对象

您的 xml 将如下所示

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="cities_images">
        <item>@drawable/capetown</item>
        <item>@drawable/newyork</item>
        <item>@drawable/losangeles</item>
    </string-array>
</resources>

您的活动代码将是这样的

TypedArray imgs = getResources().obtainTypedArray(R.array.cities_images);
// get resource ID by index
imgs.getResourceId(i, -1)
// or set you ImageView's resource to the id
mImgView1.setImageResource(imgs.getResourceId(i, -1));
// recycle the array
imgs.recycle();

最新更新