2013-06-17 46 views
7

Tôi đã viết mã, trong đó tôi đang hiển thị hình ảnh trong GridView, nhưng bây giờ tôi muốn hiển thị văn bản ở cuối mỗi hình ảnh.Cách hiển thị Văn bản trên Hình ảnh

Và tôi muốn hiển thị lưới của tôi như thế này:

enter image description here

nhưng nhận được như thế này [cũng muốn hiển thị văn bản cho hình ảnh]:

enter image description here

chính. xml:

<?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" 
    > 
    <GridView 
     android:id="@+id/gridview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:verticalSpacing="0dp" 
     android:horizontalSpacing="0dp" 
     android:stretchMode="columnWidth" 
     android:numColumns="2" 
     /> 
</FrameLayout> 
+0

các câu hỏi ở đây là gì? –

+2

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

+0

đồng ý với @ Android-Developer – Chetan

Trả lời

7

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); 
+0

Đây sẽ là cách hiệu quả nhất để thực hiện những gì bạn muốn , vì việc sử dụng bộ điều hợp tùy chỉnh cho phép bạn tái chế chế độ xem và tùy chỉnh nội dung của mình. –

Các vấn đề liên quan