【问题标题】:GridView not coming up Full Screen in AndroidGridView在Android中没有出现全屏
【发布时间】:2015-04-12 17:02:17
【问题描述】:

奇怪的是,我正在实现自己的日历,gridView 并没有完全出现。我的数字正常,但屏幕未满。这是一半。我想实现类似于 CalendarView 的东西。

请检查一下你会明白的快照:

你能看到很多空的灰色空间吗?怎么做到全屏的?

这里有两个与日历相关的xml

CalendarView.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/calendarMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/calendar_top" >
    </RelativeLayout>

    <GridView
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/header"
        android:layout_gravity="center_horizontal"
        android:listSelector="@android:color/transparent"
        android:numColumns="7"
        android:stretchMode="columnWidth" />

</RelativeLayout>

接下来是 CalendarItems.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/calendar_cell"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="2dip" >

    <TextView
        android:id="@+id/date"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="#000000"
        android:textSize="16dip" >
    </TextView>

    <TextView
        android:id="@+id/textvaluedate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/date"
        android:textColor="#F44336"
        android:textSize="10dip"
        android:visibility="gone" >
    </TextView>

</LinearLayout>

尝试更改所有 match_parent 仍然相同。我真的不确定我哪里出错了?

有人可以帮我解决这个问题吗?

谢谢!

更新 2:

我已根据 @Heshan Sandeepa 更新了我的代码。我得到了全屏,但图像框看起来真的很大。请看一下屏幕截图。这里有什么问题?

这是我的 Baseadapter 中的代码:

DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
        int height = metrics.heightPixels;

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, month.get(Calendar.YEAR));
        calendar.set(Calendar.MONTH, month.get(Calendar.MONTH));
        int numDays = calendar.getActualMaximum(Calendar.DATE);

        double rowCounts = Math.ceil(numDays/7);
        int rowCount = (int) rowCounts;

        if (convertView == null) 
        {  
            LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.calendar_item, null);
        }

        v.setMinimumHeight(height / rowCount);
}

我只需要每个日期都需要更大的方框。就是这样。

告诉我!

谢谢!

【问题讨论】:

  • 我不确定,但您应该尝试在日历项目上设置布局权重,以便它们根据需要填充尽可能多的空间。
  • 怎么样?我的意思是日历项是动态添加的,对吗​​?
  • CalendarItems xml Linearlayout 中的 android:layout_weight="1" 之类的东西可能会起作用。
  • 正在尝试......希望它有效。
  • @JonasCz:试过了..没用。 :(

标签: java android xml android-relativelayout


【解决方案1】:

对于列,您可以通过 strechMode read here 实现上述要求。对于行,您可以通过以下步骤实现此目的,

  1. 计算设备高度。

    getWindowManager().getDefaultDisplay().getMetrics(new DisplayMetrics());
    int deviceHeight = new DisplayMetrics().heightPixels;
    
  2. 减少操作栏高度/任何内边距(如果有的话)

  3. 您可以确定需要多少行,然后您可以定义每行的高度(实际上是单元格)。您可以通过将相关月份的天数除以7来获得行号,并获得上限。

    前-一月

    int rowCount = Math.ceil(31/7);
                 = 5
    

    ex - 2 月(小心,这里不需要获取上限,因为您可以获得整数)

    int rowCount = Math.ceil(28/7);
                 = 4
    

所有月份都一样。

  1. 现在您有了屏幕高度(第 2 步)和行数(第 3 步),因此可以定义每个 ro 的确切高度。我希望您对每个单元格都有一个适配器和自定义视图。 当您在 getView() 为自定义视图充气时,将计算出的高度定义为最小高度。

    your_view.setMinimumHeight(<device height / rowCount>)
    

【讨论】:

  • 这是一个日历:所以 Sun to Sat 最好显示 7 列,对吧?
  • 嘿!抱歉,我正在旅行,无法使用您的新逻辑。我今天才应用它并获得了全屏,但请看一下屏幕截图。它看起来真的很大。这里有什么问题?我的意思是我想为每个数字获得更大的方框。我已经用 SNAP Shot 更新了我的问题
  • 请告诉我!谢谢!
  • 你没有计算出我认为的正确高度。您尝试了哪个月份?
  • 四月份我试过了。我完全按照你告诉我的去做
猜你喜欢
  • 1970-01-01
  • 2018-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多