2017-10-07 62 views
4

我創建了一個應用程序,它列出了gridview中SD卡上的歌曲,但gridview單元格不是正方形大小,我也想限制顯示在單元格中的文本不超過一行或15-20個字符。我嘗試過在這裏提到的一個問題,但它不適合我。此外它限制了佈局的高度。我該如何解決這個問題?如何限制gridview中的文本長度,並使單元格正方形

PlayListActivity.java

public class PlayListActivity extends Activity { 

    private String[] mAudioPath; 
    private String[] mMusicList; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_play_list); 

     GridView mListView = (GridView) findViewById(android.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 { 
        Intent i= new Intent(PlayListActivity.this,Player.class); 
        i.putExtra("anything",arg2); 
        i.putExtra("whatever",mAudioPath); 
        startActivity(i); 
       } catch (IllegalArgumentException e) { 
        e.printStackTrace(); 
       } catch (IllegalStateException e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 

     mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
      @Override 
      public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) { 

       for (int j = 0; j < adapterView.getChildCount(); j++) 
        adapterView.getChildAt(j).setBackgroundColor(Color.TRANSPARENT); 

       // change the background color of the selected element 
       view.setBackgroundColor(Color.LTGRAY); 
       return true; 
      } 
     }); 
    } 


    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; 
    } 
} 

MyLinearLayout.java

import android.content.Context; 
import android.util.AttributeSet; 
import android.widget.LinearLayout; 

public class MyLinearLayout extends LinearLayout { 
    public MyLinearLayout(Context context) { 
     super(context); 
    } 

    public MyLinearLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public MyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 
    @Override 
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, widthMeasureSpec); // This is the key that will make the height equivalent to its width 
    } 
} 

activity_play_list.xml:

<?xml version="1.0" encoding="utf-8"?> 
<com.example.dell_1.myapp3.MusicPlayer.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 

    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/btn_playlist" /> 

    <GridView 
     android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:numColumns="2" 
     android:layout_height="match_parent" 
     android:divider="#242424" 
     android:dividerHeight="1dp" 
     /> 

</com.example.dell_1.myapp3.MusicPlayer.MyLinearLayout> 

This is the end result I am getting

回答

1

您應該爲此使用自定義適配器。

創建一個擴展了ArrayAdapter的類,然後覆蓋getView方法。裏面有設置沒有TextView的的行:

@Override 
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) { 
    View view = super.getView(position, convertView, parent); 
    TextView textView = (TextView) view.findViewById(android.R.id.text1); 
    textView.setLines(5); 
    return view; 
} 

在使用這個新的類,而不是ArrayAdapter

0

你的情況後,你可以限制歌曲的名稱長度代替

songs[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)); 

String songName = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)); 
songs[i] = songName.substring(0, Math.min(songName.length(), 25)); 
//now 25 is the length of every songs name 

這樣每首歌的名字長度都是相等的,而且henc每個細胞的大小相等。 但我建議你使用自定義適配器,以便您可以從項目的xml文件做到這一點,其中TextView

<TextView 
    android:id="@+id/text" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ellipsize="end" 
    android:maxLines="5"/> 
    //Setting max line of your text view to five and ... after that 

或程序

your_text_view.setEllipsize(TextUtils.TruncateAt.END); 
your_text_view.setMaxLines(5); 
//Setting max line of your text view to five and ... after that 
相關問題