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

时间:2016-01-17 12:28:12

标签: android xml android-resources

如何在android?

中以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;">

1 个答案:

答案 0 :(得分:1)

您应该在array.xml文件夹中的res/values文件中使用TypedArray来创建其他资源的数组,例如drawables

您的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();