未显示自定义搜索栏

时间:2017-05-17 01:45:20

标签: android xml hide seekbar invisible

我想将自定义搜索栏直接放在上面的播放操作栏上方。 我创建了一个自定义搜索栏并将其附加到LinearLayout。

设计屏幕在执行前显示搜索栏。 但是,如果我在真实设备上运行该应用程序将不会显示搜索栏。

以下是该活动和自定义搜索栏的xml文件。

seekbar1_seekbar.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
    <shape android:shape="line">
        <stroke
            android:width="6dp"
            android:color="#D5D5D5" />
    </shape>
</item>
<item android:id="@android:id/secondaryProgress">
    <clip>
        <shape android:shape="line">
            <stroke android:width="6dp" android:color="#B2CCFF"/>
        </shape>
    </clip>
</item>

<item android:id="@android:id/progress">
    <clip>
        <shape android:shape="line">
            <stroke android:width="6dp" android:color="#6799FF"/>
        </shape>
    </clip>
</item>
</layer-list>

seekbar1_thumb1.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#D5D5D5"></solid>
<size
    android:width="6dp"
    android:height="6dp"/>
</layer-list>

activity_main.xml中

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:thumb="@drawable/seekbar1_thumb1"
        android:progressDrawable="@drawable/seekbar1_seekbar"
        android:progress="80"
        android:max="100"
        android:paddingLeft="0dp"
        android:paddingRight="0dp"
        android:thumbOffset="0dp"/>

    <LinearLayout
        android:id="@+id/lin_miniplayer"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@android:color/darker_gray"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/img_albumart"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginLeft="5dp"
            android:scaleType="fitXY"
            android:src="@drawable/empty_albumart"/>

        <TextView
            android:id="@+id/txt_title"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:text="Sample Title"
            android:maxLines="1"
            android:ellipsize="end"
            android:textColor="@android:color/black"/>

        <ImageButton
            android:id="@+id/btn_repeat"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:background="?attr/selectableItemBackground"
            android:src="@drawable/repeat_off" />

        <ImageButton
            android:id="@+id/btn_rewind"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:background="?attr/selectableItemBackground"
            android:src="@drawable/rewind"/>

        <ImageButton
            android:id="@+id/btn_play_pause"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:background="?attr/selectableItemBackground"
            android:src="@drawable/play"/>

        <ImageButton
            android:id="@+id/btn_forward"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:background="?attr/selectableItemBackground"
            android:src="@drawable/forward"/>

        <ImageButton
            android:id="@+id/btn_shuffle"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:background="?attr/selectableItemBackground"
            android:src="@drawable/shuffle_disabled"/>
    </LinearLayout>
</LinearLayout>

请告知。

添加活动源。

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private final static int LOADER_ID = 0x001;
private static boolean chk = false;

private SeekBar sb;
private RecyclerView mRecyclerView;
private AudioAdapter mAdapter;
private ImageView mImgAlbumArt;
private TextView mTxtTitle;
private ImageButton mBtnPlayPause;
private ImageButton mBtnSuffle;
private ImageButton mBtnRepeat;
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        updateUI();
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if(ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1000);
        } else {
            getAudioListFromMediaDatabase();
        }
    } else {
        getAudioListFromMediaDatabase();
    }

    mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
    mAdapter = new AudioAdapter(this, null);
    mRecyclerView.setAdapter(mAdapter);
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    mRecyclerView.setLayoutManager(layoutManager);

    mImgAlbumArt = (ImageView) findViewById(R.id.img_albumart);
    mTxtTitle = (TextView) findViewById(R.id.txt_title);
    mBtnPlayPause = (ImageButton) findViewById(R.id.btn_play_pause);
    mBtnSuffle = (ImageButton) findViewById(R.id.btn_shuffle);
    mBtnRepeat = (ImageButton) findViewById(R.id.btn_repeat);
    findViewById(R.id.lin_miniplayer).setOnClickListener(this);
    findViewById(R.id.btn_rewind).setOnClickListener(this);
    mBtnPlayPause.setOnClickListener(this);
    mBtnSuffle.setOnClickListener(this);
    mBtnRepeat.setOnClickListener(this);
    findViewById(R.id.btn_forward).setOnClickListener(this);

    sb = (SeekBar) findViewById(R.id.seekBar1);
    registerBroadcast();
}

private void registerBroadcast(){
    IntentFilter filter = new IntentFilter();
    filter.addAction(BroadcastActions.PREPARED);
    filter.addAction(BroadcastActions.PLAY_STATE_CHANGED);
    filter.addAction(BroadcastActions.PLAY_ORDER_CHANGED);
    filter.addAction(BroadcastActions.PLAY_REPEAT_CHANGED);
    registerReceiver(mBroadcastReceiver, filter);
}

private void unregisterBroadcast(){
    unregisterReceiver(mBroadcastReceiver);
}

@Override
protected void onDestroy(){
    super.onDestroy();
    unregisterBroadcast();
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.lin_miniplayer:
            break;
        case R.id.btn_rewind:
            AudioApplication.getInstance().getServiceInterface().rewind();
            break;
        case R.id.btn_play_pause:
            AudioApplication.getInstance().getServiceInterface().togglePlay();
            break;
        case R.id.btn_forward:
            AudioApplication.getInstance().getServiceInterface().forward();
            break;
        case R.id.btn_repeat:
            AudioApplication.getInstance().getServiceInterface().repeat();
            chk = true;
            break;
        case R.id.btn_shuffle:
            AudioApplication.getInstance().getServiceInterface().shuffle();
            break;
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
        getAudioListFromMediaDatabase();
    }
}

private void getAudioListFromMediaDatabase() {
    getSupportLoaderManager().initLoader(LOADER_ID, null, new LoaderManager.LoaderCallbacks<Cursor>(){
        @Override
        public Loader<Cursor> onCreateLoader(int id, Bundle args) {
            Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            String[] projection = new String[]{
                    MediaStore.Audio.Media._ID,
                    MediaStore.Audio.Media.TITLE,
                    MediaStore.Audio.Media.ARTIST,
                    MediaStore.Audio.Media.ALBUM,
                    MediaStore.Audio.Media.ALBUM_ID,
                    MediaStore.Audio.Media.DURATION,
                    MediaStore.Audio.Media.DATA
            };
            String selection = MediaStore.Audio.Media.IS_MUSIC + " = 1";
            String sortOrder = MediaStore.Audio.Media.TITLE + " COLLATE LOCALIZED ASC";

            return new CursorLoader(getApplicationContext(), uri, projection, selection, null, sortOrder);
        }

        @Override
        public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
            Log.d("onLoadFinished", data.toString());
            mAdapter.swapCursor(data);

        }

        @Override
        public void onLoaderReset(Loader<Cursor> loader) {
            mAdapter.swapCursor(null);
        }
    });
}



private void updateUI(){
    if(AudioApplication.getInstance().getServiceInterface().isRepeat() == 0 && chk){
        mBtnRepeat.setImageResource(R.drawable.repeat_once);
    } else if(AudioApplication.getInstance().getServiceInterface().isRepeat() == 1){
        mBtnRepeat.setImageResource(R.drawable.repeat);
    } else if(AudioApplication.getInstance().getServiceInterface().isRepeat() == 2){
        mBtnRepeat.setImageResource(R.drawable.repeat_off);
    }

    if(AudioApplication.getInstance().getServiceInterface().isShuffle()) {
        mBtnSuffle.setImageResource(R.drawable.shuffle_disabled);
    } else {
        mBtnSuffle.setImageResource(R.drawable.shuffle);
    }

    if(AudioApplication.getInstance().getServiceInterface().isPlaying()) {
        mBtnPlayPause.setImageResource(R.drawable.pause);
    }
    else{
         mBtnPlayPause.setImageResource(R.drawable.play);
    }
    AudioAdapter.AudioItem audioItem = AudioApplication.getInstance().getServiceInterface().getAudioItem();
    if(audioItem != null){
        Uri albumArtUri = ContentUris.withAppendedId(Uri.parse("content://media/external/audio/albumart"), audioItem.mAlbumId);
        Picasso.with(getApplicationContext()).load(albumArtUri).error(R.drawable.empty_albumart).into(mImgAlbumArt);
        mTxtTitle.setText(audioItem.mTitle);
    } else {
        mImgAlbumArt.setImageResource(R.drawable.empty_albumart);
        mTxtTitle.setText("재생중인 음악이 없습니다.");
    }
}

}

0 个答案:

没有答案