Android gridview android:numColumns =" auto_fit"总是只创建两列 [英] Android gridview android:numColumns="auto_fit" always create only two columns

查看:208
本文介绍了Android gridview android:numColumns =" auto_fit"总是只创建两列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我正在开发小型Android应用程序,我想用一些元素来显示简单的gridview。它工作正常。唯一的问题是,即使有空间,它总是只显示两列。它将屏幕平分为2列,只显示两个元素。如果我将列数设置为数字,即不是auto_fit,则显示正确。我的代码如下所示:

 < FrameLayout xmlns:android =http://schemas.android.com/apk/res / android
xmlns:tools =http://schemas.android.com/tools
android:layout_width =match_parent
android:layout_height =match_parent
>
< GridView
android:id =@ + id / gridView
android:layout_width =fill_parent
android:layout_height =fill_parent
android: layout_margin =5dp
android:numColumns =auto_fit
android:verticalSpacing =10dp
android:horizo​​ntalSpacing =10dp>
< / GridView>
< / FrameLayout>

并且我的网格元素看起来像:

 < RelativeLayout xmlns:android =http://schemas.android.com/apk/res/android
xmlns:tools =http://schemas.android .com / tools
android:layout_width =wrap_content
android:layout_height =wrap_content
tools:context =com.example.androidcardlayout.MainActivity>

< android.support.v7.widget.CardView
xmlns:card_view =http://schemas.android.com/apk/res-auto
android: id =@ + id / card_view
android:layout_width =100dp
android:layout_height =150dp
card_view:cardCornerRadius =4dp>

RelativeLayout
android:id =@ + id / cardMianRlt
android:layout_width =100dp
android:layout_height =150dp
>
< / RelativeLayout>




我是做错了什么?需要一些帮助。谢谢。

解决方案

看起来像自适应设置只适用于固定列宽的情况。这是GridView源代码中唯一使用自适应设置的地方:

  private boolean determineColumns(int availableSpace){
final int requestedHorizo​​ntalSpacing = mRequestedHorizo​​ntalSpacing;
final int stretchMode = mStretchMode;
final int requestedColumnWidth = mRequestedColumnWidth;
布尔值didNotInitiallyFit = false;

if(mRequestedNumColumns == AUTO_FIT){
if(requestedColumnWidth> 0){
//客户告诉我们选择列数
mNumColumns =( availableSpace + requestedHorizo​​ntalSpacing)/
(requestedColumnWidth + requestedHorizo​​ntalSpacing);
} else {
//如果我们没有足够的信息,只需填写一个数字
mNumColumns = 2;






这个私有函数在度量/布局过程中被调用。请注意,在auto-fit if语句中,除非 requestedColumnWidth> 0 ,那么你只会得到2列,这就是你所看到的。



如果固定宽度适用于您的应用程序,那么您需要像这样将它放入XML中:

 < FrameLayout xmlns:android =http://schemas.android.com/apk/res/android
xmlns:tools =http://schemas.android .com / tools
android:layout_width =match_parent
android:layout_height =match_parent
>
< GridView
android:id =@ + id / gridView
android:layout_width =fill_parent
android:layout_height =fill_parent
android: layout_margin =5dp
android:numColumns =auto_fit
android:columnWidth =30dp
android:verticalSpacing =10dp
android:horizo​​ntalSpacing =10dp> ;
< / GridView>
< / FrameLayout>


Hi I am developing small android application in which I want to display simple gridview with some elements.Its working properly. Only problem is that it is always showing only two columns even though there is space. Its is equally dividing screen into 2 columns and displaying only two elements.If I set number of columns as number i.e. not auto_fit then it is showing properly. My code looks like:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
 >
<GridView
    android:id="@+id/gridView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="5dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp">
</GridView>
</FrameLayout>

and my grid element looks like:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context="com.example.androidcardlayout.MainActivity" >

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="100dp"
    android:layout_height="150dp"
    card_view:cardCornerRadius="4dp">

    <RelativeLayout 
        android:id="@+id/cardMianRlt"
        android:layout_width="100dp"
        android:layout_height="150dp"
        >
    </RelativeLayout>

Am I doing anything wrong? Need Some help. Thank you.

解决方案

It looks like the auto-fit setting only applies if you have fixed column widths. Here is the only place in GridView source code that uses the auto-fit setting:

private boolean determineColumns(int availableSpace) {
    final int requestedHorizontalSpacing = mRequestedHorizontalSpacing;
    final int stretchMode = mStretchMode;
    final int requestedColumnWidth = mRequestedColumnWidth;
    boolean didNotInitiallyFit = false;

    if (mRequestedNumColumns == AUTO_FIT) {
        if (requestedColumnWidth > 0) {
            // Client told us to pick the number of columns
            mNumColumns = (availableSpace + requestedHorizontalSpacing) /
                    (requestedColumnWidth + requestedHorizontalSpacing);
        } else {
            // Just make up a number if we don't have enough info
            mNumColumns = 2;
        }
    }

This private function is called during the measure / layout process. Notice that within the auto-fit if statement, unless requestedColumnWidth > 0, then you just get 2 columns, which is what you are seeing.

If a fixed width works for your app, then you need to put it in your XML like this:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >
    <GridView
        android:id="@+id/gridView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_margin="5dp"
        android:numColumns="auto_fit"
        android:columnWidth="30dp"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp">
    </GridView>
</FrameLayout>

这篇关于Android gridview android:numColumns =&quot; auto_fit&quot;总是只创建两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆