所以我正在尝试创建一个音频播放器应用程序,在其中,播放按钮工作正常,无需设置任何背景,但只要我在XML文件中设置背景,ImageButton就变得不可点击了。那么有人可以帮助我吗?谢谢
XML文件:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:weightSum="1"
android:descendantFocusability="blocksDescendants">
<RelativeLayout
android:layout_width="300dp"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_bright">
<ImageButton
android:id="@+id/btnPrevious"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_weight="2.40"
android:background="@null"
android:paddingLeft="10dp"
android:src="@drawable/btn_previous" />
<ImageButton
android:id="@+id/btnPlay1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_centerInParent="true"
android:background="@drawable/btn_play" />
<ImageButton
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:paddingLeft="230dp"
android:src="@drawable/btn_next" />
</RelativeLayout>
</LinearLayout>
btn_play.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/download4"
android:state_pressed="true" />
<item android:drawable="@drawable/az"/>
</selector>
PlayListActivity.java:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_list);
mMediaPlayer = new MediaPlayer();
ListView mListView = (ListView) findViewById(R.id.list);
mMusicList = getAudioList();
ArrayAdapter<String> mAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, mMusicList);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
try {
playSong(mAudioPath[arg2]);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private String[] getAudioList() {
final Cursor mCursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DATA}, null, null,
"LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");
int count = mCursor.getCount();
String[] songs = new String[count];
mAudioPath = new String[count];
int i = 0;
if (mCursor.moveToFirst()) {
do {
songs[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
mAudioPath[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
i++;
} while (mCursor.moveToNext());
}
mCursor.close();
return songs;
}
private void playSong(String path) throws IllegalArgumentException,
IllegalStateException, IOException {
setContentView(R.layout.activity_android_building_music_player);
Log.d("ringtone", "playSong :: " + path);
mMediaPlayer.reset();
mMediaPlayer.setDataSource(path);
//mMediaPlayer.setLooping(true);
mMediaPlayer.prepare();
mMediaPlayer.start();
acv(path);
abc();
}
public void acv(String path) {
getInit();
metaRetriver = new MediaMetadataRetriever();
metaRetriver.setDataSource(path);
try {
art = metaRetriver.getEmbeddedPicture();
Bitmap songImage = BitmapFactory.decodeByteArray(art, 0, art.length);
album_art.setImageBitmap(songImage);
album.setText(metaRetriver
.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM));
artist.setText(metaRetriver
.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST));
} catch (Exception e) {
album_art.setBackgroundColor(Color.GRAY);
album.setText("Unknown Album");
artist.setText("Unknown Artist");
}
}
public void getInit() {
album_art = (ImageView) findViewById(R.id.coverart1);
album = (TextView) findViewById(R.id.Album);
artist = (TextView) findViewById(R.id.artist_name);
}
public void abc(){
ImageButton btnPlay1 = (ImageButton) findViewById(R.id.btnPlay1);
btnPlay1.setOnClickListener(
new ImageButton.OnClickListener(){
public void onClick(View v){
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
} else {
mMediaPlayer.start();
}
}
});
}
答案 0 :(得分:0)
使用此
更改btn_play.xml<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/download4"
android:state_pressed="false" />
<item android:drawable="@drawable/az"
android:state_pressed="true" />
<item android:drawable="@drawable/az"
android:state_selected="true" />
</selector>
答案 1 :(得分:0)
试试这个:
<item android:drawable="@drawable/download4"
android:state_pressed="true" />
<item android:drawable="@drawable/az"
android:state_focused="true" />
<item android:drawable="@drawable/az" />
并删除此内容:
android:background="@null"
android:clickable="true"
改变这个:
android:background="@drawable/btn_play"
修改RelativeLayout,如下所示:
<RelativeLayout
android:layout_width="300dp"
android:layout_height="match_parent">
<ImageButton
android:id="@+id/btnPrevious"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="@drawable/btn_play" />
<ImageButton
android:id="@+id/btnPlay1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/btn_play" />
<ImageButton
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/btn_play" />
</RelativeLayout>
答案 2 :(得分:0)
在您的按钮中设置此android:clickable="true"
属性。试试这个
<ImageButton
android:id="@+id/btnPlay1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:clickable="true"
android:background="@drawable/btn_play" />
要创建<selector>
到<ImageButton>
请Check这个。