2016-11-16 480 views
0

我想伸展我的GridView,其中包含每个网格内的ImageViewTextView,以覆盖手机的完整屏幕。Stretch GridView适合全屏

我已经尝试了很多在线的解决方案,但没有一个为我工作。其中一些我已经尝试过的答案是:

How can I force a GridView to use the whole screen (regardless of display size)?

android How to stretch rows in the gridview to fill screen?

这里是我的代码:

activity_layout.xml

<FrameLayout 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@drawable/gaming"> 
    <GridView 
     android:id="@+id/categoryGridView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:verticalSpacing="1dp" 
     android:horizontalSpacing="1dp" 
     android:background="@android:color/transparent" 
     android:numColumns="2" 
     android:stretchMode="columnWidth" 
     android:layout_weight="1" /> 
</FrameLayout> 

category_grid_single.xml

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/relativelayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<ImageView 
    android:id="@+id/gridImageView" 
    android:layout_width="60dip" 
    android:layout_height="60dip" 
    android:scaleType="centerCrop" 
    android:gravity="center" 
    android:layout_centerInParent="true" 
    android:layout_margin="1dp"/> 
<TextView 
    android:id="@+id/gridImageViewText" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/gridImageView" 
    android:layout_margin="5dp" 
    android:gravity="center" 
    android:textSize="20sp" 
    android:textStyle="bold" 
    android:layout_centerHorizontal="true" 
    android:textColor="@color/colorWhite" /> 
</RelativeLayout> 

CustomAdapter.java

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    View grid; 
    if (convertView == null) { 

     LayoutInflater inflater = (LayoutInflater) mContext 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     grid = inflater.inflate(R.layout.main_category_grid_single, null); 
     grid.setMinimumHeight(mheight/3);//mheight is the Height of the screen 

    } else { 
     grid = convertView; 
    } 
    TextView textView = (TextView) grid.findViewById(R.id.gridImageViewText); 
    ImageView imageView = (ImageView)grid.findViewById(R.id.gridImageView); 
    textView.setText(category[position]); 
    imageView.setImageResource(ImageId[position]); 
    return grid; 
} 

我想实现一个类似于下面图片GridView。但目前设置我的GridView后,我得到了底部的空白空间。如果除GridView之外还有其他更好的方法可以实现,那么请提出建议。

enter image description here

这是我得到

enter image description here

任何帮助将不胜感激。提前致谢!

+0

为您的文本视图做的android:layout_width = “match_parent” 如果不工作给予重量1,看 – Redman

+0

@Redman,我没有工作。同样的结果。 –

+0

你可以保留截图如何得到它可能有帮助吗? – Redman

回答

2

通过使用layout_columnWeightlayout_rowWeight属性,可以使用GridLayout而不是GridView来实现此目的。在API级别14中添加了GridLayout,但这些属性直到API级别21才被添加。但是你可以利用GridLayout的还有layout_columnWeightlayout_rowWeight使用v7 GridLayout Support Library属性回API级别7:

compile 'com.android.support:gridlayout-v7:25.0.1' 

然后,所有你需要做的就是确保在GridLayout每个单元都有一个等于layout_columnWeightlayout_rowWeight

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" android:layout_height="match_parent"> 

    <android.support.design.widget.AppBarLayout android:id="@+id/app_bar" 
     android:layout_width="match_parent" android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar android:id="@+id/toolbar" 
      android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" 
      app:popupTheme="@style/AppTheme.PopupOverlay"/> 

    </android.support.design.widget.AppBarLayout> 

    <android.support.v7.widget.GridLayout 
     android:id="@+id/recycler" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:columnCount="2" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

     <FrameLayout 
      app:layout_columnWeight="1" 
      app:layout_rowWeight="1" 
      android:background="@color/black_alpha_26"> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:text="YouTube"/> 

     </FrameLayout> 

     <FrameLayout 
      app:layout_columnWeight="1" 
      app:layout_rowWeight="1"> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:text="Videos"/> 

     </FrameLayout> 

     <FrameLayout 
      app:layout_columnWeight="1" 
      app:layout_rowWeight="1"> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:text="VR Apps"/> 

     </FrameLayout> 

     <FrameLayout 
      app:layout_columnWeight="1" 
      app:layout_rowWeight="1" 
      android:background="@color/black_alpha_26"> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:text="VR Movies"/> 

     </FrameLayout> 

     <FrameLayout 
      app:layout_columnWeight="1" 
      app:layout_rowWeight="1" 
      android:background="@color/black_alpha_26"> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:text="VR Games"/> 

     </FrameLayout> 

     <FrameLayout 
      app:layout_columnWeight="1" 
      app:layout_rowWeight="1"> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:text="Shop"/> 

     </FrameLayout> 

    </android.support.v7.widget.GridLayout> 

</android.support.design.widget.CoordinatorLayout> 

这是我得到的结果是:

Test GridLayout

+0

谢谢!它现在确实解决了我的问题。 –

0

清单回顾一下要点为Bryan's nice solution描述网格:

  1. 添加编译指示

    • 目前正在使用:compile 'com.android.support:gridlayout-v7:25.3.1'
  2. 更新布局be android.support.v7.widget.GridLayout

    • 看在这里,我错过了在第一和做出省略完整的包名
  3. 设置app:columnCount="2"

  4. 的错误设置每个格栅元素都具有:

    app:layout_columnWeight="1" 
    app:layout_rowWeight="1" 
    

演示XML:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.GridLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:columnCount="2" 
    app:rowCount="2" 
    app:orientation="horizontal" 
    > 

    <TextView 
     android:id="@+id/m1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_gravity="center" 
     app:layout_columnWeight="1" 
     app:layout_rowWeight="1" 
     android:text="m1"/> 


    <TextView 
     android:id="@+id/m2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_gravity="center" 
     app:layout_columnWeight="1" 
     app:layout_rowWeight="1" 
     android:text="m2"/> 


    <TextView 
     android:id="@+id/m3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_gravity="center" 
     app:layout_columnWeight="1" 
     app:layout_rowWeight="1" 
     android:text="m3"/> 


    <TextView 
     android:id="@+id/m4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_gravity="center" 
     app:layout_columnWeight="1" 
     app:layout_rowWeight="1" 
     android:text="m4"/> 


</android.support.v7.widget.GridLayout>