2017-07-08 60 views
2

我正在制作音乐播放器,并在歌曲和艺术家名称中实施了选取框。但只要查询栏更新,文本视图就会回到原始位置。这样就可以看到文本的来回运动。请帮助我。选框不能使用搜索栏更新。 (Android)

下面是XML代码

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/mainFullPlayerContainer" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 

    tools:context="com.musicplayer.integrated.sanket.music.MainFullPlayer" 
    android:background="@color/defaultBackground" 

> 


<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/relativeLayout" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentTop="true"> 

    <ImageView 
     android:id="@+id/imageView_full_player_more" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentEnd="true" 
     android:layout_marginTop="20dp" 
     android:layout_marginRight="15dp" 
     app:srcCompat="@drawable/icon_more" /> 
</RelativeLayout> 

<ImageView 
    android:id="@+id/imageView_full_player_album_art" 
    android:layout_width="300dp" 
    android:layout_height="300dp" 
    app:srcCompat="@drawable/default_album_art" 
    android:layout_marginTop="29dp" 
    android:layout_below="@+id/relativeLayout" 
    android:layout_centerHorizontal="true" /> 



<SeekBar 
    android:id="@+id/seekBar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 

    android:progressTint="@color/defaultTextColor" 
    android:thumbTint="@color/defaultTextColor" 
    android:layout_above="@+id/imageView_full_player_play" 

    android:layout_marginBottom="37dp" /> 

<ImageView 
    android:id="@+id/imageView_full_player_play" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="31dp" 
    app:srcCompat="@drawable/icon_play_small" /> 

<ImageView 
    android:id="@+id/imageView_full_player_next" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/imageView_full_player_play" 
    android:layout_marginStart="39dp" 
    android:layout_toEndOf="@+id/imageView_full_player_play" 
    app:srcCompat="@drawable/icon_fast_next_small" /> 

<ImageView 
    android:id="@+id/imageView_full_player_prev" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/imageView_full_player_play" 
    android:layout_marginEnd="38dp" 
    android:layout_toStartOf="@+id/imageView_full_player_play" 
    app:srcCompat="@drawable/icon_rewind_prev_small" /> 

<TextView 
    android:id="@+id/textView_full_player_song" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignEnd="@+id/imageView_full_player_album_art" 
    android:layout_alignStart="@+id/imageView_full_player_album_art" 
    android:layout_below="@+id/imageView_full_player_album_art" 
    android:layout_marginTop="12dp" 
    android:ellipsize="marquee" 
    android:focusable="true" 
    android:marqueeRepeatLimit="marquee_forever" 
    android:scrollHorizontally="true" 
    android:singleLine="true" 
    android:text="Song Name" 
    android:textAlignment="center" 
    android:textColor="@color/defaultTextColor" 
    android:textSize="18sp" /> 

<TextView 
    android:id="@+id/textView_current_time" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="0:00" 
    android:textColor="@color/defaultTextColor" 
    android:textSize="12sp" 

    android:layout_marginBottom="18dp" 
    android:layout_above="@+id/imageView_full_player_prev" 
    android:layout_alignStart="@+id/textView_full_player_artist" /> 

<TextView 
    android:id="@+id/textView_total_length" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="5:00" 
    android:textSize="12sp" 
    android:textColor="@color/defaultTextColor" 
    android:layout_alignBaseline="@+id/textView_current_time" 
    android:layout_alignBottom="@+id/textView_current_time" 
    android:layout_alignEnd="@+id/textView_full_player_artist" /> 

<TextView 
    android:id="@+id/textView_full_player_artist" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignEnd="@+id/textView_full_player_song" 
    android:layout_alignStart="@+id/textView_full_player_song" 
    android:layout_below="@+id/textView_full_player_song" 
    android:layout_marginTop="12dp" 
    android:text="Artist" 
    android:textAlignment="center" 
    android:textColor="@color/defaultTextColor" 
    android:textSize="15sp" 
    android:ellipsize="marquee" 
    android:singleLine="true" 
    android:marqueeRepeatLimit="marquee_forever" 
    android:focusable="true" 
    android:scrollHorizontally="true" 
    /> 

    </RelativeLayout> 

Here is the design

下面是Java代码的

public class MainFullPlayer extends AppCompatActivity { 

private static RelativeLayout fullPlayer; 
private static ImageView fullPlayer_play , fullPlayer_next , 
fullPlayer_prev,fullPlayer_album; 
private static TextView fullPlayer_song , fullPlayer_artist , 
fullPlayer_currentTime , fullPlayer_maxTime; 
private static SeekBar seekBar; 
private Songs song; 
private static Handler progressHandler; 
private static Runnable progressRunnable; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main_full_player); 
    fullPlayer = (RelativeLayout)findViewById(R.id.mainFullPlayerContainer); 
    fullPlayer_play = (ImageView)findViewById(R.id.imageView_full_player_play); 
    fullPlayer_next = (ImageView)findViewById(R.id.imageView_full_player_next); 
    fullPlayer_prev = (ImageView)findViewById(R.id.imageView_full_player_prev); 
    fullPlayer_album = (ImageView)findViewById(R.id.imageView_full_player_album_art); 
    fullPlayer_song = (TextView)findViewById(R.id.textView_full_player_song); 
    fullPlayer_artist = (TextView)findViewById(R.id.textView_full_player_artist); 
    fullPlayer_currentTime = (TextView)findViewById(R.id.textView_current_time); 
    fullPlayer_maxTime = (TextView)findViewById(R.id.textView_total_length); 

    fullPlayer_maxTime.setText(MusicPlayback.getTime(MusicPlayback.mediaPlayer.getDuration())); 

    seekBar = (SeekBar)findViewById(R.id.seekBar); 

    song = MusicPlayback.allTracks.get(MusicPlayback.songPosition); 
    fullPlayer_song.setSelected(true); 
    fullPlayer_artist.setSelected(true); 
    fullPlayer_song.setText(song.getSongName()); 
    fullPlayer_artist.setText(song.getArtist()); 


    if(MusicPlayback.getPlayingStatus()){ 
     fullPlayer_play.setImageResource(R.drawable.icon_pause_small); 
    } 
    else 
    { 
     fullPlayer_play.setImageResource(R.drawable.icon_play_small); 
    } 
    if(song.getAlbumArt()!= null){ 

     fullPlayer_album.setImageURI(Uri.parse(song.getAlbumArt())); 
     fullPlayer.setBackground(FragmentAllTracks.d); 
    } 
    else{ 
     fullPlayer_album.setImageResource(R.drawable.default_album_art); 
     fullPlayer.setBackgroundColor(Color.argb(255, 48, 48, 48)); 

    } 
seekBar.setMax(MusicPlayback.mediaPlayer.getDuration()); 


    progressHandler = new Handler(); 

    progressRunnable = new Runnable() { 
     @Override 
     public void run() { 
      seekBar.setProgress(MusicPlayback.mediaPlayer.getCurrentPosition()); 
      fullPlayer_currentTime.setText(MusicPlayback.getTime(MusicPlayback.mediaPlayer.getCurrentPosition())); 


      progressHandler.postDelayed(progressRunnable,100); 
     } 
    }; 
    progressHandler.postDelayed(progressRunnable,100); 
} 


@Override 
public void onBackPressed() { 
    super.onBackPressed(); 
    this.overridePendingTransition(R.anim.left_exit_translate,R.anim.right_exit_translate); 

} 




} 

下面是结果

Unable to get Marquee

+0

加上'seekBar.setOnSeekBarChangeListener()'监听器,选中此https://stackoverflow.com/questions/9481977/android-seekbar-to-control-mediaplaye r-progress – Shailesh

+0

请向我推荐如何获得选取框效果。仔细阅读说明。 –

回答

0

我认为这是因为重点。请设置您的TextView这样

<TextView 
    android:id="@+id/textView_full_player_song" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignEnd="@+id/imageView_full_player_album_art" 
    android:layout_alignStart="@+id/imageView_full_player_album_art" 
    android:layout_below="@+id/imageView_full_player_album_art" 
    android:layout_marginTop="12dp" 
    android:singleLine="true" 
    android:ellipsize="marquee" 
    android:marqueeRepeatLimit ="marquee_forever" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:scrollHorizontally="true" 
    android:text="Song Name" 
    android:textAlignment="center" 
    android:textColor="@color/defaultTextColor" 
    android:textSize="18sp" /> 

添加此您的TextView,

android:focusable="true" 
android:focusableInTouchMode="true" 
+0

先生好心地阅读说明,并建议我采取一些方法来摆脱我的问题。我正在使用文本视图来显示歌手和艺人的名字。为了更新搜索栏我正在使用处理程序线程。但是在线程执行后,我无法在TextView上获得选取框效果。 –

+0

请检查我的最新答案编号 – Shailesh

+0

不幸的是,这并没有帮助我。 :(我仍然无法获得搜索栏的线程执行选取框效果。 –

0

尝试使用这个MarqueeTextView类,它会自动设置固定大小TextView,所以它不会重新测量(的原因原因重启滚动动画):

public class MarqueeTextView extends AppCompatTextView implements View.OnLayoutChangeListener { 
    public MarqueeTextView(Context context) { 
     this(context, null); 
    } 

    public MarqueeTextView(Context context, AttributeSet attrs) { 
     this(context, attrs, 0); 
    } 

    public MarqueeTextView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     setSingleLine(); 
     setEllipsize(TextUtils.TruncateAt.MARQUEE); 
     setMarqueeRepeatLimit(-1); 
     setSelected(true); 

     addOnLayoutChangeListener(this); 
    } 

    @Override 
    public boolean isFocused() { 
     return true; 
    } 

    @Override 
    public void onWindowFocusChanged(boolean hasWindowFocus) { 
     if (hasWindowFocus) super.onWindowFocusChanged(hasWindowFocus); 
    } 

    @Override 
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { 
     if (focused) super.onFocusChanged(focused, direction, previouslyFocusedRect); 
    } 

    @Override 
    public void onLayoutChange(View v, int left, int top, int right, int bottom, 
           int oldLeft, int oldTop, int oldRight, int oldBottom) { 
     ViewGroup.LayoutParams layoutParams = getLayoutParams(); 
     layoutParams.height = bottom - top; 
     layoutParams.width = right - left; 
     removeOnLayoutChangeListener(this); 
     setLayoutParams(layoutParams); 
    } 
}