Android 为什么此布局可以在预览中使用,但不能在我的设备上使用?

Android 为什么此布局可以在预览中使用,但不能在我的设备上使用?,android,android-studio,android-gridlayout,Android,Android Studio,Android Gridlayout,我正在为学校开发一个Android应用程序。我设计的布局在Android Studio预览版上看起来不错,但一旦我将其部署到我的物理设备(HTC One m8,Android 5.0.1)上,cardview内部的一切都乱七八糟 布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmln

我正在为学校开发一个Android应用程序。我设计的布局在Android Studio预览版上看起来不错,但一旦我将其部署到我的物理设备(HTC One m8,Android 5.0.1)上,cardview内部的一切都乱七八糟

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="de.seschi98.trgapp.activities.ScheduleActivity"
    tools:showIn="@layout/activity_schedule">



    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="120dp">


        <GridLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:columnCount="4"
            android:rowCount="1"
            android:columnOrderPreserved="false"
            android:orientation="horizontal"
            android:rowOrderPreserved="false">

            <TextView
                android:id="@+id/lesson_number"
                android:text="01"
                android:textSize="22dp"
                android:layout_width="wrap_content"
                android:layout_column="0"
                android:layout_row="0"
                android:layout_rowWeight="1"
                android:gravity="center_vertical|center_horizontal"
                android:textStyle="bold"
                android:layout_columnWeight="20" />

            <GridLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_column="1"
                android:layout_row="0"
                android:columnCount="2"
                android:layout_columnWeight="90"
                android:rowCount="3"
                android:layout_height="match_parent">


                <TextView
                    android:gravity="center_vertical|center_horizontal"
                    android:id="@+id/lesson_name"
                    android:text="Chemistry"
                    android:textSize="22dp"
                    android:layout_height="wrap_content"
                    android:layout_column="1"
                    android:layout_row="0"
                    android:layout_rowWeight="15"
                    android:layout_gravity="center_horizontal"
                    android:paddingTop="10dp"
                    android:layout_width="match_parent" />

                <TextView
                    android:gravity="bottom|center_horizontal"
                    android:id="@+id/teacher"
                    android:text="Mr Smith"
                    android:layout_height="wrap_content"
                    android:textSize="16dp"
                    android:layout_column="1"
                    android:layout_row="1"
                    android:layout_rowWeight="20"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="match_parent" />


                <TextView
                    android:gravity="top|center_horizontal"
                    android:id="@+id/room"
                    android:text="Room A-123"
                    android:textSize="16dp"
                    android:layout_height="wrap_content"
                    android:layout_column="1"
                    android:layout_row="2"
                    android:layout_rowWeight="20"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="match_parent" />


            </GridLayout>

            <View android:layout_row="0"
                android:layout_column="2"
                android:layout_height="match_parent"
                android:layout_width="0dp"
                android:layout_columnWeight="15">

            </View>

            <View android:layout_row="0"
                android:id="@+id/badge"
                android:layout_column="3"
                android:layout_height="match_parent"
                android:layout_width="0dp"
                android:background="#c0392b"
                android:layout_columnWeight="5">

            </View>


        </GridLayout>

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

</LinearLayout>

(动作栏或其他东西不重要,它只是CardView…)


//更新:昨天把我的Android版本升级到了6.0.0棉花糖,现在运行良好。但我包括了AppCompat的东西。。。Android studio中的预览有时与您的设备不同,请确保设置相同的手机屏幕大小,并且Android studio中的预览已经给出了答案

有时预览显示良好,但在某些设备上看起来不同。这通常是因为屏幕大小和(或)屏幕分辨率

可以为屏幕大小添加指定布局。但你必须明白android在各种各样的设备和屏幕上运行。在某些屏幕上,你总是会遇到麻烦,也许专注于一些设计上的小问题不是解决的办法

检查以下链接:

编辑:


如果你在每台设备上都有相同的问题,那么这可能不是我们已经告诉你的。让我再检查一下您的代码,我会告诉您一些其他信息。

不过,这是一个版本错误:

经过一些测试之后,我意识到我需要将布局文件中的
更改为
。没有考虑到这一点,因为我以为GridLayout会与Android 5兼容

所以现在它在我所有的设备上都可以正常工作了,包括安卓5和安卓6。
虽然这对我没有多大帮助,但谢谢你的快速回答

对齐问题,me也有同样的问题。我只设置了一个属性paddingLeft,但在某些设备中它不起作用,因此我必须在样式中同时提供属性paddingStart和paddingLeft

<item name="android:paddingLeft">@dimen/drawable_icon_padding_left</item>
<item name="android:paddingStart">@dimen/drawable_icon_padding_left</item>
@dimen/drawable\u图标\u填充\u左侧
@尺寸/可拉伸图标\u填充\u左侧

因此,请确保在布局或样式中同时使用这两个属性。快乐编码:-)

你在任何其他设备或模拟器上检查过这个问题吗?在我的三星Android 5.0.2平板电脑上也有同样的问题…我不相信这是屏幕大小,因为我使用了列和行的权重,所以它应该适合屏幕大小…@seschi98-这不是引导:)@LinX64是的,我知道,但我认为将gridLayout宽度设置为与父项匹配(可以说是100%)并在子项上定义权重可以让它有点响应。尝试声明填充和布局边界,但我正在使用权重,不设置任何静态宽度/高度值。。。。我不明白为什么这很重要…这帮我解决了问题-谢谢。但是,为了能够使用网格属性,如
app:columnCount=“2”
,我还需要设置它:
xmlns:app=”http://schemas.android.com/apk/lib/android.support.v7.widget.GridLayout“