为什么此布局在预览中有效但在我的设备上无效?
Why does this layout work in preview but not on my device?
我正在为学校开发 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>
Device Screenshot(与操作栏或其他东西无关,只有 CardView...)
// 更新:昨天将我的 Android 版本升级到 6.0.0 Marshmallow,现在可以正常工作了。但是我包括了 AppCompat 东西......它不能在以前版本的 Android 上运行的原因可能是什么?
android studio 中的预览有时与您的设备不同,请确保您的 phone 屏幕尺寸与 android studio
中的预览设置相同
Kenny已经给你答案了
有时预览显示正常,但在某些设备上看起来不一样。这通常是因为屏幕尺寸和(或)您的屏幕分辨率。
您可以为屏幕尺寸添加指定布局。但是您必须了解 android 可以在各种设备和屏幕尺寸上运行。你总是会在某些屏幕上遇到麻烦,也许专注于小的设计问题不是解决问题的方法。
检查此链接:
Android And Supporting Mutiple Screens Layouts
编辑:
如果您在每台设备上都遇到同样的问题,那么这可能不是我们已经告诉过的问题。让我再检查一下你的代码,然后我会告诉你一些其他的事情。
没关系,这是版本错误:
经过一些测试后,我意识到我需要将布局文件中的 <GridLayout>
更改为 <android.support.v7.widget.GridLayout>
。没想到因为我认为 GridLayout 会与 Android 5...
兼容
所以现在它在我的所有设备上都运行良好,Android 5 和 Android 6 也是如此。
虽然对我帮助不大,但还是感谢您的快速解答!
对齐很重要,我也遇到了同样的问题。我只设置了一个 属性 paddingLeft 但在某些设备上它不起作用所以我不得不同时提供 属性 paddingStart 和 paddingLeft 样式-
<item name="android:paddingLeft">@dimen/drawable_icon_padding_left</item>
<item name="android:paddingStart">@dimen/drawable_icon_padding_left</item>
因此请确保您在布局或样式中同时使用 属性。编码愉快:-)
我正在为学校开发 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>
Device Screenshot(与操作栏或其他东西无关,只有 CardView...)
// 更新:昨天将我的 Android 版本升级到 6.0.0 Marshmallow,现在可以正常工作了。但是我包括了 AppCompat 东西......它不能在以前版本的 Android 上运行的原因可能是什么?
android studio 中的预览有时与您的设备不同,请确保您的 phone 屏幕尺寸与 android studio
中的预览设置相同Kenny已经给你答案了
有时预览显示正常,但在某些设备上看起来不一样。这通常是因为屏幕尺寸和(或)您的屏幕分辨率。
您可以为屏幕尺寸添加指定布局。但是您必须了解 android 可以在各种设备和屏幕尺寸上运行。你总是会在某些屏幕上遇到麻烦,也许专注于小的设计问题不是解决问题的方法。
检查此链接:
Android And Supporting Mutiple Screens Layouts
编辑:
如果您在每台设备上都遇到同样的问题,那么这可能不是我们已经告诉过的问题。让我再检查一下你的代码,然后我会告诉你一些其他的事情。
没关系,这是版本错误:
经过一些测试后,我意识到我需要将布局文件中的 <GridLayout>
更改为 <android.support.v7.widget.GridLayout>
。没想到因为我认为 GridLayout 会与 Android 5...
所以现在它在我的所有设备上都运行良好,Android 5 和 Android 6 也是如此。 虽然对我帮助不大,但还是感谢您的快速解答!
对齐很重要,我也遇到了同样的问题。我只设置了一个 属性 paddingLeft 但在某些设备上它不起作用所以我不得不同时提供 属性 paddingStart 和 paddingLeft 样式-
<item name="android:paddingLeft">@dimen/drawable_icon_padding_left</item>
<item name="android:paddingStart">@dimen/drawable_icon_padding_left</item>
因此请确保您在布局或样式中同时使用 属性。编码愉快:-)