2012-06-27 43 views
8

Tôi có một số GridView và mỗi ô có ImageView với TextView bên dưới. Thật không may nếu TextView có nhiều hơn một dòng văn bản bị cắt. Tôi đã thử tất cả mọi thứ nhưng tôi không thể tìm thấy một giải pháp.Chế độ xem toàn màn hình Gridview bị cắt

Dường như chiều cao hàng của GridView là vấn đề chứ không phải văn bản thực tế vì bạn có thể thấy một nửa văn bản trong số Textview.

Đây là mã của tôi:

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

    GridView gridView = (GridView) findViewById(R.id.gridView1); 
    gridView.setAdapter(new ImageAdapter()); 

    gridView.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> arg0, View v, int position, long id) { 
      startActivity(new Intent(Main.this, Acronyms.class)); 
     } 

    }); 
} 

public class ImageAdapter extends BaseAdapter { 

    private Integer[] iconImg = { 
      R.drawable.acronyms, R.drawable.acronyms, 
      R.drawable.acronyms 
    }; 
    private String[] iconTitle = { 
      "Acronyms", "Cardiac Strips", 
      "Test 3" 
    }; 

    public int getCount() { 
     return iconImg.length; 
    } 

    public Object getItem(int arg0) { 
     return null; 
    } 

    public long getItemId(int arg0) { 
     return 0; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     View view = null; 
     if (convertView == null) { 
      LayoutInflater inflater = getLayoutInflater(); 
      view = inflater.inflate(R.layout.icon, null); 
      TextView textView = (TextView) view.findViewById(R.id.icon_text); 
      textView.setText(iconTitle[position]); 
      ImageView imageView = (ImageView) view.findViewById(R.id.icon_image); 
      imageView.setImageResource(iconImg[position]); 

     } else { 
      view = convertView; 
     } 

     return view; 
    } 
} 

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#a3e6ff" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:paddingBottom="10dp" 
     android:paddingTop="10dp" 
     android:shadowDx="0" 
     android:shadowDy="0" 
     android:shadowRadius="25" 
     android:text="Example" 
     android:textColor="#248fb7" 
     android:textSize="40dp" 
     android:typeface="serif" android:gravity="center_horizontal"/> 

    <GridView 
     android:id="@+id/gridView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:columnWidth="90dp" 
     android:horizontalSpacing="10dp" 
     android:numColumns="3" 
     android:stretchMode="columnWidth" 
     android:verticalSpacing="15dp" > 
    </GridView> 

</LinearLayout> 

và icon.xml tôi

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/grid_icon_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center_horizontal" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/icon_image" 
     android:layout_width="90dp" 
     android:layout_height="wrap_content" /> 


    <TextView 
     android:id="@+id/icon_text" 
     android:layout_width="90dp" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:maxLines="2" 
     android:singleLine="false" 
     android:text="Samples Samples" 
     android:textColor="#FFFFFF" 
     android:textSize="15dp" 
     android:typeface="serif" /> 

</LinearLayout> 

Và đây là một ảnh chụp màn hình: Multi-line cut off

+0

Tôi đang gặp vấn đề chính xác cùng ! Cực kỳ khó chịu! –

Trả lời

2

[EDIT1]

Bạn có thể thử sử dụng RelativeLayout thay vì Bố cục tuyến tính cho biểu tượng.xml.

Nếu điều này không hoạt động thì tôi sẽ chuyển sang một TextView chiều cao tĩnh. Từ khi nhìn vào ảnh chụp màn hình của bạn, có vẻ như bạn sẽ luôn sử dụng cùng một hình ảnh và văn bản sẽ là 1 dòng hoặc 2. Chỉ cần tạo chiều cao văn bản tĩnh để cho phép 2 dòng.

[ORIGINAL] Tôi nghĩ rằng sự cố nằm trong định nghĩa bố cục tuyến tính của bạn cho biểu tượng.xml của bạn. Trong định nghĩa của bạn, bạn có bố cục có "match_parent" làm tham số chiều rộng và chiều cao. Bạn nên, vì đây là về cơ bản được subviews trong GridView được "wrap_content". Dưới đây là những gì tôi nghĩ rằng nó phải

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:id="@+id/grid_icon_layout"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:gravity="center_horizontal"  
    android:orientation="vertical" > 

    <ImageView android:id="@+id/icon_image"   
     android:layout_width="90dp"   
     android:layout_height="wrap_content" />  

    <TextView android:id="@+id/icon_text"   
     android:layout_width="90dp"   
     android:layout_height="wrap_content"   
     android:gravity="center_horizontal"   
     android:maxLines="2"   
     android:singleLine="false"   
     android:text="Samples Samples"   
     android:textColor="#FFFFFF"   
     android:textSize="15dp"   
     android:typeface="serif" /> 

</LinearLayout> 
+0

Cảm ơn bạn đã phản hồi nhanh! Tôi đã thử điều này và tôi vẫn nhận được kết quả tương tự mặc dù. – meepz

+0

Tôi đã kết thúc việc thiết lập rõ ràng chiều cao của TextView trong tệp tin icon.xml. Tôi đã cố gắng tương đối và tuyến tính và không có may mắn. GridView phải gặp khó khăn khi hiển thị vì mục đầu tiên trong lưới là một dòng, nó có thể đi theo chiều cao mục đó. – meepz

+0

Tôi nghĩ rằng điều xảy ra là bố cục con tổng thể kết thúc với độ cao khác nhau trong khung nhìn (ví dụ: mỗi mục lưới có thể có chiều cao khác nhau). Tôi thực sự không biết làm thế nào mã nền của GridView được lập trình, nhưng tôi tự hỏi nếu bạn đã xác định 2 dòng văn bản của bạn đầu tiên, nếu nó sẽ kích thước một cách thích hợp? Nó có thể có một cái gì đó để làm với nó bằng cách sử dụng đầu tiên cho chiều cao chiều cao và không bao giờ cập nhật sau đó !!! – trumpetlicks

5

tôi giải quyết sử dụng, khi tôi định nghĩa nó

TextView.setLines(2); 

trong xml TextView là

android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
Các vấn đề liên quan