【发布时间】:2016-04-18 20:15:58
【问题描述】:
我想做一个音板。为此,我制作了一个 listview 和一个 xml 文件(用于名称和声音)。
首先,我制作了一个带有声音标题的列表视图。当我们点击它时,就会播放声音。
主活动:
mSoundPlayer = new SoundPlayer(this);
Sound[] soundArray = SoundStore.getSounds(this);
ListView listView = (ListView) findViewById(R.id.listView);
final ArrayAdapter<Sound> adapter =
new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, soundArray);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
Sound sound = (Sound) parent.getItemAtPosition(position);
mSoundPlayer.playSound(sound);
}
});
我使用xml 文件来索引声音的名称和声音。
文件:arrays.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="labels">
<item>First sound</item>
<item>Second soung</item>
</string-array>
<integer-array name="ids">
<item>@raw/a1</item>
<item>@raw/a2</item>
</integer-array>
</resources>
最后,我的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
tools:context="com.clemb.sardboard.MainActivity">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listView"
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="100dp"
android:verticalSpacing="5dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
我只想知道如何在声音名称前添加图片。我可以在我的 array.xml 中做吗?
谢谢。
【问题讨论】:
标签: android xml listview android-listview