【问题标题】:GridLayout in NestedScrollView , why do my columns go outside the view space?NestedScrollView 中的 GridLayout ,为什么我的列会超出视图空间?
【发布时间】:2016-04-14 07:52:07
【问题描述】:

基本上当我在 android studio 中创建一个新项目时,所有默认活动都包含一个 NestedScrollView。

如果我尝试将网格布局作为其唯一的子项,则列会超出屏幕大小。您可以在下图中看到:

这是添加一个与 (1,1) 中的瓶子相同的图像按钮,注意按钮的宽度是 wrap_content,但没有内容(酒是一个方形图标,设置为 src,具有 fitStart 比例),但由于某种原因,它覆盖了整个屏幕尺寸。

我尝试了多种方法使其工作,阅读 stackoverflow 我明白我应该使用 android.support.v7.widget.GridLayout 而不是网格布局,所以我做到了。然后我读到我应该添加一个线性布局作为父级,出于某种原因,但没有运气。

这是我的 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="it.giuseppi.alessandro.wine.Main">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<android.support.v7.widget.GridLayout
    android:layout_width="match_parent"
    android:layout_height="900dp"
    app:columnCount="3">

    <ImageButton
        android:layout_height="150dp"
        app:layout_column="0"
        android:id="@+id/imageButton"
        android:src="@drawable/premium"
        android:scaleType="fitStart"
        android:background="@color/yellow"
        android:baselineAlignBottom="false"
        app:layout_row="0"/>

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="150dp"
        android:id="@+id/imageButton2"
        android:src="@drawable/premium"
        android:scaleType="fitStart"
        app:layout_column="1"
        app:layout_row="1" />

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

</android.support.v4.widget.NestedScrollView>

如果需要,我可以提供我的整个项目。

【问题讨论】:

    标签: android android-layout android-studio


    【解决方案1】:

    检查您的行设置和行大小。

     app:layout_column="1"
        app:layout_row="1" />
    

    行宽等于设备宽度。这意味着任何其他水平放置的项目都会移到屏幕右侧。

    【讨论】:

      猜你喜欢
      • 2015-04-01
      • 1970-01-01
      • 2021-11-02
      • 1970-01-01
      • 2016-02-01
      • 2015-07-30
      • 1970-01-01
      • 2018-04-10
      • 1970-01-01
      相关资源
      最近更新 更多