2012-01-02 38 views
19

Chế độ xem lưới của ứng dụng của tôi có 3 hàng và 3 cột. Tôi muốn điều này để lấp đầy màn hình, bất kể kích thước màn hình của thiết bị.Chế độ xem lưới trên lưới Android điều chỉnh theo kích thước màn hình

Tôi đã thử nhận kích thước cửa sổ và đặt bố cục tương ứng. Nhưng điều này không đưa ra một sự liên kết hoàn hảo vì nó hoạt động với weightsum trong linearlayout.

Vì vậy, chúng ta có thể sử dụng trọng số cho GridView hoặc có bất kỳ thuộc tính Android nào khác có thể được sử dụng. Cảm ơn rất nhiều về thời gian và phản hồi.

<GridView 
       xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/gridview" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_below="@+id/text1view" 
       android:background="@drawable/home_bg" 
       android:gravity="center" 
       android:horizontalSpacing="10dip" 
       android:numColumns="3" 
       android:stretchMode="columnWidth" /> 

từng hạng mục lưới là

<?xml version="1.0" encoding="utf-8"?> 

<ImageView 
    android:id="@+id/grid_item_image" 
    android:layout_width="wrap_content" 
    android:layout_height="75dip" 
    android:layout_margin="0dip" 
    android:padding="0dip" > 
</ImageView> 

<TextView 
    android:id="@+id/grid_item_text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_below="@+id/grid_item_image" 
    android:gravity="center_horizontal" 
    android:padding="0dip" 
    android:textColor="#000000" 
    android:textSize="10sp" > 
</TextView> 

mã của adapter:

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 

    if (convertView == null) { 
     LayoutInflater mInflater = (LayoutInflater) mContext 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = mInflater.inflate(R.layout.griditems, null); 
     holder = new ViewHolder(); 
     holder.text1 = (TextView) convertView 
       .findViewById(R.id.grid_item_text); 

     holder.image = (ImageView) convertView 
       .findViewById(R.id.grid_item_image); 

     // if it's not recycled, initialize some attributes 
     holder.image.setImageResource(gridItemIds[position]); 
     holder.text1.setText(gridTitles[position]); 
     int h = mContext.getResources().getDisplayMetrics().densityDpi; 

     // holder.image.setLayoutParams(new LayoutParams(h-50,h-50)); 
     convertView.setLayoutParams(new GridView.LayoutParams(h - 45, 
       h - 39)); 
     // holder.image.setScaleType(ImageView.ScaleType.CENTER_CROP); 
     // holder.image.setScaleType(ImageView.ScaleType.FIT_XY); 
     // holder.image.setPadding(20, 20, 20, 20); 

     // convertView.setLayoutParams(new GridView.LayoutParams(Utils 
     // .getLayoutParameter(), Utils.getLayoutParameter()+50)); 

     holder.image.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View view) { 
       new ModuleManager().startModuleACtivity(position, mContext); 
      } 
     }); 
     convertView.setTag(holder); 
    } else { 

     holder = (ViewHolder) convertView.getTag(); 

    } 

    return convertView; 
} 

static class ViewHolder { 
    TextView text1; 
    ImageView image; 

} 

vui lòng xem dòng

convertView.setLayoutParams(new GridView.LayoutParams(h-45, h-39)); 

tôi đã đạt được giá trị này với một số thử nghiệm trên trình giả lập kích thước khác nhau. Nhưng tôi không nghĩ rằng nó là một cách đúng đắn để xác định chiều cao và chiều rộng.

+0

Hiện một số mã. Bạn đang cô găng lam gi? – TryTryAgain

+0

tôi đã thêm. cho thuê có một cái nhìn. Cảm ơn thời gian ur và giúp đỡ – png

+0

Cùng một vấn đề .. Bạn có thể giúp tôi, làm thế nào bạn ra khỏi này. – Sreeram

Trả lời

15

Theo như tôi đã hiểu câu hỏi của bạn, vì bạn chưa đăng bất kỳ mã nào, những gì bạn đang cố gắng làm là, bạn muốn GridView xuất hiện trên toàn màn hình.

Trước hết bạn phải sử dụng một số bố trí để đặt GridView của bạn trong Làm như sau:.

chiều cao Make và chiều rộng của bố trí để điền vào mẹ:

android:layout_width="fill_parent" 
android:layout_height="fill_parent" 

sau đó, thiết lập chiều cao và chiều rộng của gridview của bạn để fill_parent là tốt.

Vâng, đây chỉ là một giải pháp được cho là bạn có thể đã làm việc theo cách này. đặt một số mã sẽ tốt hơn nhiều. Hy vọng điều này làm việc cho bạn.

Cập nhật: Vâng vấn đề có thể nằm ở đây:

convertView.setLayoutParams(new GridView.LayoutParams(h-45, h-39)); 

thay đổi:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT); 
convertView.setLayoutParams(new GridView.LayoutParams(params)); 

Hope làm việc này cho bạn.

+0

tôi đã thêm vào. cho thuê có một cái nhìn. Cảm ơn bạn đã dành thời gian và giúp đỡ – png

+0

@ Preetha ... xem câu trả lời được cập nhật. –

+0

hoạt động của nó .. cảm ơn bạn rất nhiều – png

10

@preetha: trước hết hãy xem ví dụ này tôi đã sử dụng điều này và bố cục của tôi không ảnh hưởng đến bất kể chiều cao và chiều rộng ...

http://developer.android.com/resources/tutorials/views/hello-gridview.html

thiết lập sau đây trong xml

android:id="@+id/gridview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:columnWidth="90dp" 
    android:numColumns="auto_fit" 
    android:verticalSpacing="10dp" 
    android:horizontalSpacing="10dp" 
    android:stretchMode="columnWidth" 
    android:gravity="center" 

hy vọng điều này giúp ...

+1

Cảm ơn bạn. Tôi đã thử điều này, không hoạt động. Đối với tôi, không: cols phải là ba. Ngoài ra lưới của tôi là một lần xem hình ảnh + xem văn bản – png

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