2

我有一個ListView,我希望當我點擊一個ListView項目時,底部的TextView會改變它的值,如果文本太大,它會自動滾動。Horizo​​ntalScrollView剪切TextView中的文本並自動滾動

1)Horizo​​ntalScrollView切斷文字,原文是「恩裏克·伊格萊西亞斯 - Bailando英尺德斯坎默·布埃諾,根特德卵」,但沒有空間,所以我只看到「恩裏克·伊格萊西亞斯 - Bailando英尺 」。現在,我想用滾動我也看到「Descemer Bueno,Gente De Zona」,但沒有... TextView的值是正確的,有全名,我看到日誌...

我看到:

enter image description here

我看一下TextView的:

enter image description here

2)自動滾動不起作用,我必須點擊TextView進行滾動。

這是我的代碼佈局:

titleSongPlayngTextView = (TextView) rootView.findViewById(R.id.song_title); 
    titleSongPlayngTextView.setMovementMethod(new ScrollingMovementMethod()); 

    final HorizontalScrollView s = (HorizontalScrollView) 
    rootView.findViewById(R.id.title_scroller); 
    s.fullScroll(HorizontalScrollView.FOCUS_RIGHT); 

這是我的代碼佈局(把TextView的在Ho​​rizo​​ntalScrollView標籤沒有作品滾動,還帶有點擊):

<LinearLayout 
     android:id="@+id/info_playing_song" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="0.82" 
     android:orientation="vertical"> 

     <HorizontalScrollView 
      android:id="@+id/title_scroller" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

     </HorizontalScrollView > 

     <TextView 
      android:id="@+id/song_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:maxLines="1" 
      android:foregroundGravity="left" 
      android:text="Title Song" 
      android:singleLine="true" 
      android:ellipsize="marquee" 
      android:marqueeRepeatLimit ="marquee_forever" 
      android:focusable="true" 
      android:focusableInTouchMode="true" 
      android:scrollHorizontally="true" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textColor="?android:attr/textColorPrimary" /> 

UPDATE:

在我的片段中我有:

public void setMediaButtons(View rootView) { 
    albumSongPlayngImageView = (ImageView) rootView.findViewById(R.id.album_song); 

    titleSongPlayngTextView = (TextView) rootView.findViewById(R.id.song_title); 
    titleSongPlayngTextView.setMovementMethod(new ScrollingMovementMethod()); 
    titleSongPlayngTextView.setText(firstSong.getTitle()); 
    titleSongPlayngTextView.setSelected(true); 
    Log.d(LOG_TAG, "After scroll clicked song: " + titleSongPlayngTextView.getText()); 
     //*************** HERE the TextView value is correct*************** 

    artistSongPlayngTextView = (TextView) rootView.findViewById(R.id.song_name_artist); 
    artistSongPlayngTextView.setMovementMethod(new ScrollingMovementMethod()); 
    artistSongPlayngTextView.setSelected(true); 
    artistSongPlayngTextView.setText(firstSong.getArtist()); 

在MusicService我:

public void playSong() { 
    mediaPlayer = musicPlayer.getMediaPlayer(); 
    mediaPlayer.setOnPreparedListener(this); 

    mediaPlayer.reset(); 
    Song currPlaySong = arrayOfSongs.get(currentSongPosition); 
    long currSong = currPlaySong.getID(); 
    Uri trackUri = ContentUris.withAppendedId(
      android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
      currSong); 

    //set the data source 
    try { 
     mediaPlayer.setDataSource(getApplicationContext(), trackUri); 

     songTitle = currPlaySong.getTitle(); 
     titleSongPlayngTextView.setText(songTitle); 

     Log.d(LOG_TAG, "TextView value: " + titleSongPlayngTextView.getText()); 
     //*************** HERE the TextView value is correct*************** 

     songArtist = currPlaySong.getArtist(); 
     artistSongPlayngTextView.setText(songArtist); 


    } catch (Exception e) { 
     Log.e("MUSIC SERVICE", "Error setting data source", e); 
    } 
    mediaPlayer.prepareAsync(); 
} 

最後,這是我修改的佈局(也許有問題嗎?另一個問題是,如果TextView的累計值Si太大,我得到一個非常小的ImageView):

<?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"> 

    <ListView 
     android:id="@+id/list_songs" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@+id/fragment_button_player" /> 

    <LinearLayout 
     android:id="@+id/fragment_button_player" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:background="@android:color/darker_gray" 
     android:orientation="horizontal" 
     android:paddingBottom="5dp" 
     android:weightSum="1"> 

     <ImageView 
      android:id="@+id/album_song" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="0.21" 
      android:src="@drawable/ic_album_black_24dp" /> 

     <LinearLayout 
      android:id="@+id/info_playing_song" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="0.82" 
      android:orientation="vertical"> 


      <TextView 
       android:id="@+id/song_title" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:maxLines="1" 
       android:foregroundGravity="left" 
       android:text="Title Song" 
       android:singleLine="true" 
       android:ellipsize="marquee" 
       android:marqueeRepeatLimit ="marquee_forever" 
       android:focusable="true" 
       android:focusableInTouchMode="true" 
       android:scrollHorizontally="true" 
       android:textAppearance="?android:attr/textAppearanceMedium" 
       android:textColor="?android:attr/textColorPrimary" /> 

       <TextView 
        android:id="@+id/song_name_artist" 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" 
        android:ellipsize="marquee" 
        android:focusable="true" 
        android:focusableInTouchMode="true" 
        android:marqueeRepeatLimit="marquee_forever" 
        android:maxLines="1" 
        android:scrollHorizontally="true" 
        android:singleLine="true" 
        android:text="Artist Name" 
        android:textAppearance="?android:attr/textAppearanceSmall" /> 

     </LinearLayout> 

     <ImageButton 
      android:id="@+id/prev_song" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:foregroundGravity="center_vertical|bottom|right" 
      android:padding="8dp" 
      android:src="@drawable/ic_skip_previous_black_24dp" 
      android:tintMode="src_in" /> 

     <ImageButton 
      android:id="@+id/play_pause" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:foregroundGravity="center_vertical|bottom|right" 
      android:padding="8dp" 
      android:src="@drawable/ic_play_arrow_black_24dp" 
      android:tintMode="src_in" /> 

     <ImageButton 
      android:id="@+id/next_song" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:foregroundGravity="center_vertical|bottom|right" 
      android:padding="8dp" 
      android:src="@drawable/ic_skip_next_black_24dp" 
      android:tintMode="src_in" /> 

    </LinearLayout> 

</RelativeLayout> 

這裏是ImageView的problema屏幕,只有在它具有真正的維度的第一圖像,其他所有被縮放爲TextView值太大。我總是想要像第一幅圖像一樣暗淡,真實的暗淡。

解決方案爲TextView的價值切斷在這裏看到:Scrolling TextView value are cut off

enter image description here

+0

請不要更新您的問題,以包括從答案中取得的調整,從而產生一個新問題。可以添加輔助原始問題的其他信息。請看這裏http://meta.stackoverflow.com/questions/290297/how-much-change-to-the-question-is-too-much –

+0

好的,謝謝,我在這裏寫了另一個問題:http:// stackoverflow .com/questions/33570512/scrolling-textview-value-are-cut-off – Hazlo8

回答

1

默認情況下,選取框只會滾動的onfocus。因此,如果您將「選定」設置爲「真」,它將始終集中並因此滾動。

您需要在textview上將setSelected設置爲true,以便文本滾動。

titleSongPlayngTextView.setSelected(true); 

另外,沒有沒有textview的滾動視圖就沒有意義了。你不需要使用滾動視圖,但如果你這樣做,把textview放在它裏面。

至於它切斷文本,我需要看你的代碼,因爲你有播放器控件的文本視圖。

編輯
後更多信息

的權重之和比你的更大weightsum 1

你前兩個子元素都大於1,你有沒有包括您圖像按鈕在這。

您不能使用權重,在這種情況下,權重將是所有權重的總和。否則,確保你的體重加起來到你的體重總和。同樣是我的一個疏忽,在使用佈局權重時將您的佈局寬度設置爲0dp。

<LinearLayout 
    android:id="@+id/fragment_button_player" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:orientation="horizontal" 
    android:paddingBottom="5dp" 
    android:weightSum="1"> 
    <ImageView 
     android:id="@+id/album_song" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="0.21" 
     android:src="@drawable/ic_album_black_24dp" /> 
    <LinearLayout 
     android:id="@+id/info_playing_song" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="0.82" 
     android:orientation="vertical"> 
     <TextView 
      android:id="@+id/song_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
     <TextView 
      android:id="@+id/song_name_artist" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
    </LinearLayout> 

    // These below are not included in your weightSum 

    <ImageButton 
     android:id="@+id/prev_song" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
    <ImageButton 
     android:id="@+id/play_pause" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
    <ImageButton 
     android:id="@+id/next_song" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true"    
</LinearLayout> 

所以,你會是這樣的:

<ImageView 
     android:id="@+id/album_song" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_weight="0.2" 
     android:foregroundGravity="center_vertical|bottom|left" 
     android:src="@drawable/ic_album_black_24dp"/> 

    <LinearLayout 
     android:id="@+id/info_playing_song" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.5" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/song_title" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:ellipsize="marquee" 
      android:focusable="true" 
      android:focusableInTouchMode="true" 
      android:foregroundGravity="left" 
      android:marqueeRepeatLimit="marquee_forever" 
      android:maxLines="1" 
      android:scrollHorizontally="true" 
      android:singleLine="true" 
      android:text="Title Song and too much information to display in one line." 
      android:textAppearance="?android:attr/textAppearanceMedium"/> 

     <TextView 
      android:id="@+id/song_name_artist" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:ellipsize="marquee" 
      android:focusable="true" 
      android:focusableInTouchMode="true" 
      android:marqueeRepeatLimit="marquee_forever" 
      android:maxLines="1" 
      android:scrollHorizontally="true" 
      android:singleLine="true" 
      android:text="Artist Name and too much information to read in one line." 
      android:textAppearance="?android:attr/textAppearanceSmall" /> 

    </LinearLayout> 

    <ImageButton 
     android:id="@+id/prev_song" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_weight="0.1" 
     android:foregroundGravity="center_vertical|bottom|right" 
     android:padding="8dp" 
     android:src="@drawable/ic_skip_previous_black_24dp" 
     android:tintMode="src_in"/> 

    <ImageButton 
     android:id="@+id/play_pause" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_weight="0.1" 
     android:foregroundGravity="center_vertical|bottom|right" 
     android:padding="8dp" 
     android:src="@drawable/ic_play_arrow_black_24dp" 
     android:tintMode="src_in"/> 

    <ImageButton 
     android:id="@+id/next_song" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_weight="0.1" 
     android:foregroundGravity="center_vertical|bottom|right" 
     android:padding="8dp" 
     android:src="@drawable/ic_skip_next_black_24dp" 
     android:tintMode="src_in"/> 

</LinearLayout> 

我測試過這一點,它的工作原理。你可以輕輕搖動你的體重以適應你的需求。

+0

我現在得睡覺了,請添加更多細節,以便我明天可以更充分地回答這個問題。謝謝。 –

+0

嗨,感謝您的回覆......感謝您的自動滾動,不是有效......我更新了我的問題更多代碼 – Hazlo8

+0

@Alex感謝您的編輯。有一件事是,我看不到圖像在水平滾動區域內顯示的位置......?這是上面列表中的黑色專輯圖片嗎? –

相關問題