Bạn cần phải xác định bố cục mục lưới tùy chỉnh có chứa chế độ xem văn bản để gán văn bản.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/thumbnail"
android:padding="8dp"
android:scaleType="cropCenter"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_gravity="bottom"
android:textColor="@android:color/white"
android:background="#55000000" />
</FrameLayout>
Ở đây chúng ta đã tạo ra một FrameLayout trong đó có các ImageView thiết lập để phù hợp với kích thước của phụ huynh, đây sẽ là ImageView hiển thị ảnh. Tiếp theo, có một TextView sẽ được sử dụng để hiển thị tiêu đề mục và được căn chỉnh ở dưới cùng của FrameLayout.
Tiếp theo, chúng tôi cần chỉnh sửa bộ điều hợp của bạn để sử dụng bố cục mục lưới này và hiển thị thông tin chính xác.
public View getView(int position, View convertView, ViewGroup parent) {
// Inflate the single grid item layout
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.grid_item, parent, false);
}
// Set Image
ImageView thumbnail = convertView.findViewById(R.id.thumbnail);
if (thumbnail != null) {
thumbnail.setImageResource(mThumbIds[position]);
}
// Set Text
TextView title = convertView.findViewById(R.id.title);
if (title != null) {
title.setText("Image Number: " + position);
}
return convertView;
}
mLayoutInflater cần được xác định trên toàn cầu trong constructor của bạn sử dụng
mLayoutInflater = LayoutInflater.from(context);
các câu hỏi ở đây là gì? –
tạo bố cục tùy chỉnh cho mục GridView của bạn và đặt hình ảnh đó + TextView và điền chúng vào Bộ điều hợp của bạn. – hardartcore
đồng ý với @ Android-Developer – Chetan