Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在gridview中限制文本长度并使单元格成为正方形_Java_Android_Xml_Gridview_Layout - Fatal编程技术网

Java 如何在gridview中限制文本长度并使单元格成为正方形

Java 如何在gridview中限制文本长度并使单元格成为正方形,java,android,xml,gridview,layout,Java,Android,Xml,Gridview,Layout,我创建了一个应用程序,它在gridview的SD卡上列出歌曲,但是gridview的单元格不是正方形,我还想限制单元格中显示的文本不超过一行或15-20个字符。我试着做了一个问题中提到的事情,但它对我不起作用。它还限制了布局的高度。我该如何解决这个问题 播放活动。java: public class PlayListActivity extends Activity { private String[] mAudioPath; private String[] mMusicLi

我创建了一个应用程序,它在gridview的SD卡上列出歌曲,但是gridview的单元格不是正方形,我还想限制单元格中显示的文本不超过一行或15-20个字符。我试着做了一个问题中提到的事情,但它对我不起作用。它还限制了布局的高度。我该如何解决这个问题

播放活动。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;
    }
}
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\u play\u 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>


为此,您应该使用自定义适配器

创建一个扩展
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

在您的情况下,您可以限制歌曲的名称长度

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

所以每首歌的名字的长度是相等的,所以每个单元格的大小是相等的。 但是我建议您使用一个自定义适配器,这样您就可以从项目的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
<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