Android 安卓系统的布局在某些设备上看起来乱七八糟
我在布局上遇到了一个非常奇怪的问题。它看起来就像eclipse XML编辑器和我的三星galaxy中设计的一样,但在我的旧手机xperia x10 mini中就搞砸了。我只能假设这也会发生在其他设备上 如果有人能帮我解决这个问题,我将不胜感激 下面是两个屏幕截图和XML代码 它在eclipse布局编辑器和我的三星galaxy S4 mini中的外观 索尼xperia x10 mini的外观Android 安卓系统的布局在某些设备上看起来乱七八糟,android,android-layout,android-linearlayout,android-constraintlayout,android-framelayout,Android,Android Layout,Android Linearlayout,Android Constraintlayout,Android Framelayout,我在布局上遇到了一个非常奇怪的问题。它看起来就像eclipse XML编辑器和我的三星galaxy中设计的一样,但在我的旧手机xperia x10 mini中就搞砸了。我只能假设这也会发生在其他设备上 如果有人能帮我解决这个问题,我将不胜感激 下面是两个屏幕截图和XML代码 它在eclipse布局编辑器和我的三星galaxy S4 mini中的外观 索尼xperia x10 mini的外观 这就是ImageView的风格 <style name="ta_img" >
这就是ImageView的风格
<style name="ta_img" >
<item name="android:layout_width">40dp</item>
<item name="android:layout_height">40dp</item>
<item name="android:clickable">true</item>
<item name="android:src">@drawable/ta</item>
</style>
40dp
40dp
真的
@可抽出式/ta
任何想法???使用高dp值大多会导致小屏幕失真。如果您打算支持这些设备,可以做两件事:
layout\u weight
或RelativeLayout
在我看来,这两种做法都是最好的做法,尽管第一种更重要。我怀疑您现在看到的问题是由于Xperia x10的屏幕相对较小(240x320px)。当您尝试将
layout\u-marginTop
和layout\u-marginLeft
设置为相对较高的dp
时,实际可能会超过手机的宽度/高度,从而导致您看到的布局
我建议在
RelativeLayout
中利用多个LinearLayout
来获得更具可伸缩性的布局。您可以有4个LinearLayout
s,每个都沿着屏幕的一个边缘,并且在这些布局中您可以放置ImageView
s。通过给每个ImageView
赋予相同的layou权重
,它们可以沿线性布局的长度均匀分布
您可以看到这一点。此库将帮助您根据不同的屏幕大小缩放视图
编辑:这就是库的工作方式
您可以简单地使用@dimen/_sdp
而不是您正在使用的正常dp
例如
<View android:layout_marginTop="@dimen/_15sdp" android:layout_marginLeft="@dimen/_15sdp" android:layout_height="@dimen/_200sdp" android:layout_width="@dimen/_2sdp" android:background="#B2CFEF"/>
另外请注意,我上面使用的值仅供参考。您必须尝试找出适合您需要的值。约束布局可以轻松调整以适应任何屏幕,而无需任何复杂的层次结构,如下所示:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<View
android:id="@+id/left_border"
android:layout_width="2dp"
android:layout_height="0dp"
android:layout_margin="@dimen/border_margin"
android:background="#B2CFEF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/ta_lu"
style="@style/ta_img"
app:layout_constraintLeft_toLeftOf="@id/left_border"
app:layout_constraintRight_toRightOf="@id/left_border"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/ta_lc" />
<ImageView
android:id="@+id/ta_lc"
app:layout_constraintLeft_toLeftOf="@id/left_border"
app:layout_constraintRight_toRightOf="@id/left_border"
app:layout_constraintTop_toBottomOf="@id/ta_lu"
app:layout_constraintBottom_toTopOf="@id/ta_ld"
style="@style/ta_img" />
<ImageView
android:id="@+id/ta_ld"
app:layout_constraintLeft_toLeftOf="@id/left_border"
app:layout_constraintRight_toRightOf="@id/left_border"
app:layout_constraintTop_toBottomOf="@id/ta_lc"
app:layout_constraintBottom_toBottomOf="parent"
style="@style/ta_img" />
<View
android:id="@+id/right_border"
android:layout_width="2dp"
android:layout_height="0dp"
android:layout_margin="@dimen/border_margin"
android:background="#B2CFEF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/ta_ru"
style="@style/ta_img"
app:layout_constraintLeft_toLeftOf="@id/right_border"
app:layout_constraintRight_toRightOf="@id/right_border"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/ta_rc" />
<ImageView
android:id="@+id/ta_rc"
app:layout_constraintLeft_toLeftOf="@id/right_border"
app:layout_constraintRight_toRightOf="@id/right_border"
app:layout_constraintTop_toBottomOf="@id/ta_ru"
app:layout_constraintBottom_toTopOf="@id/ta_rd"
style="@style/ta_img" />
<ImageView
android:id="@+id/ta_rd"
app:layout_constraintLeft_toLeftOf="@id/right_border"
app:layout_constraintRight_toRightOf="@id/right_border"
app:layout_constraintTop_toBottomOf="@id/ta_rc"
app:layout_constraintBottom_toBottomOf="parent"
style="@style/ta_img" />
<View
android:id="@+id/top_border"
android:layout_width="0dp"
android:layout_height="2dp"
android:layout_margin="@dimen/border_margin"
android:background="#B2CFEF"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/ta_tl"
style="@style/ta_img"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="@id/ta_tc"
app:layout_constraintTop_toTopOf="@id/top_border"
app:layout_constraintBottom_toBottomOf="@id/top_border" />
<ImageView
android:id="@+id/ta_tc"
style="@style/ta_img"
app:layout_constraintLeft_toLeftOf="@id/ta_tl"
app:layout_constraintRight_toRightOf="@id/ta_tr"
app:layout_constraintTop_toTopOf="@id/top_border"
app:layout_constraintBottom_toBottomOf="@id/top_border" />
<ImageView
android:id="@+id/ta_tr"
style="@style/ta_img"
app:layout_constraintLeft_toLeftOf="@id/ta_tc"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@id/top_border"
app:layout_constraintBottom_toBottomOf="@id/top_border" />
<View
android:id="@+id/bottom_border"
android:layout_width="0dp"
android:layout_height="2dp"
android:layout_margin="@dimen/border_margin"
android:background="#B2CFEF"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
<ImageView
android:id="@+id/ta_bl"
style="@style/ta_img"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="@id/ta_bc"
app:layout_constraintTop_toTopOf="@id/bottom_border"
app:layout_constraintBottom_toBottomOf="@id/bottom_border" />
<ImageView
android:id="@+id/ta_bc"
style="@style/ta_img"
app:layout_constraintLeft_toLeftOf="@id/ta_bl"
app:layout_constraintRight_toRightOf="@id/ta_br"
app:layout_constraintTop_toTopOf="@id/bottom_border"
app:layout_constraintBottom_toBottomOf="@id/bottom_border" />
<ImageView
android:id="@+id/ta_br"
style="@style/ta_img"
app:layout_constraintLeft_toLeftOf="@id/ta_bc"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@id/bottom_border"
app:layout_constraintBottom_toBottomOf="@id/bottom_border" />
以下是一个屏幕截图:
来自关于的官方指南 Android运行在各种不同屏幕尺寸的设备上 和密度。对于应用程序,Android系统提供了 跨设备的一致开发环境并处理大多数 将每个应用程序的用户界面调整到屏幕上的工作 它将显示在屏幕上。同时,系统提供了 允许您针对特定屏幕大小控制应用程序的UI 和密度,以优化不同用户的UI设计 屏幕配置 例如,您可能需要平板电脑的UI 这与手机的用户界面不同。虽然系统执行 扩展和调整大小以使应用程序在不同的平台上工作 屏幕,您应该努力优化您的应用程序 不同的屏幕尺寸和密度。这样做,您可以最大限度地提高 所有设备的用户体验以及您的用户相信 该应用程序实际上是为他们的设备而设计的,而不是简单的 拉伸以适合其设备上的屏幕 为了支持不同的屏幕尺寸,您必须具有不同大小的图像,这些图像将保存在不同的文件夹中 阅读 FYI ConstraintLayout负责管理定位和 可视化组件(也称为小部件)的大小调整行为 它包含
您希望此视图如何缩放?它应该始终与设备的宽度/高度大致相同,还是希望在每个设备上都保持相同的物理尺寸。我问的原因是,您当前的布局不是很动态,可能会在许多设备上看起来很奇怪。理想情况下,它会扩展到设备大小,在更大的设备上看起来更大。我知道我发布的xml无法扩展,但这只是第一步。有几个问题:1)Xperia是什么Android版本?API级别?2) 样式中可绘制的
@drawable/ta
是什么。这是你可以分享的吗?屏幕看起来很好,就像在一个带有LDPI 240x320 px屏幕的模拟器上一样。@Anonymous这个答案使用了一个正确的解决方案,具有现代布局。这也是第一次想到的,所以我制作了相同的布局,将所有dp值除以二。还是一样的结果…只是更小!我会用它贴一张照片
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<View
android:id="@+id/left_border"
android:layout_width="2dp"
android:layout_height="0dp"
android:layout_margin="@dimen/border_margin"
android:background="#B2CFEF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/ta_lu"
style="@style/ta_img"
app:layout_constraintLeft_toLeftOf="@id/left_border"
app:layout_constraintRight_toRightOf="@id/left_border"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/ta_lc" />
<ImageView
android:id="@+id/ta_lc"
app:layout_constraintLeft_toLeftOf="@id/left_border"
app:layout_constraintRight_toRightOf="@id/left_border"
app:layout_constraintTop_toBottomOf="@id/ta_lu"
app:layout_constraintBottom_toTopOf="@id/ta_ld"
style="@style/ta_img" />
<ImageView
android:id="@+id/ta_ld"
app:layout_constraintLeft_toLeftOf="@id/left_border"
app:layout_constraintRight_toRightOf="@id/left_border"
app:layout_constraintTop_toBottomOf="@id/ta_lc"
app:layout_constraintBottom_toBottomOf="parent"
style="@style/ta_img" />
<View
android:id="@+id/right_border"
android:layout_width="2dp"
android:layout_height="0dp"
android:layout_margin="@dimen/border_margin"
android:background="#B2CFEF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/ta_ru"
style="@style/ta_img"
app:layout_constraintLeft_toLeftOf="@id/right_border"
app:layout_constraintRight_toRightOf="@id/right_border"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/ta_rc" />
<ImageView
android:id="@+id/ta_rc"
app:layout_constraintLeft_toLeftOf="@id/right_border"
app:layout_constraintRight_toRightOf="@id/right_border"
app:layout_constraintTop_toBottomOf="@id/ta_ru"
app:layout_constraintBottom_toTopOf="@id/ta_rd"
style="@style/ta_img" />
<ImageView
android:id="@+id/ta_rd"
app:layout_constraintLeft_toLeftOf="@id/right_border"
app:layout_constraintRight_toRightOf="@id/right_border"
app:layout_constraintTop_toBottomOf="@id/ta_rc"
app:layout_constraintBottom_toBottomOf="parent"
style="@style/ta_img" />
<View
android:id="@+id/top_border"
android:layout_width="0dp"
android:layout_height="2dp"
android:layout_margin="@dimen/border_margin"
android:background="#B2CFEF"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/ta_tl"
style="@style/ta_img"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="@id/ta_tc"
app:layout_constraintTop_toTopOf="@id/top_border"
app:layout_constraintBottom_toBottomOf="@id/top_border" />
<ImageView
android:id="@+id/ta_tc"
style="@style/ta_img"
app:layout_constraintLeft_toLeftOf="@id/ta_tl"
app:layout_constraintRight_toRightOf="@id/ta_tr"
app:layout_constraintTop_toTopOf="@id/top_border"
app:layout_constraintBottom_toBottomOf="@id/top_border" />
<ImageView
android:id="@+id/ta_tr"
style="@style/ta_img"
app:layout_constraintLeft_toLeftOf="@id/ta_tc"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@id/top_border"
app:layout_constraintBottom_toBottomOf="@id/top_border" />
<View
android:id="@+id/bottom_border"
android:layout_width="0dp"
android:layout_height="2dp"
android:layout_margin="@dimen/border_margin"
android:background="#B2CFEF"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
<ImageView
android:id="@+id/ta_bl"
style="@style/ta_img"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="@id/ta_bc"
app:layout_constraintTop_toTopOf="@id/bottom_border"
app:layout_constraintBottom_toBottomOf="@id/bottom_border" />
<ImageView
android:id="@+id/ta_bc"
style="@style/ta_img"
app:layout_constraintLeft_toLeftOf="@id/ta_bl"
app:layout_constraintRight_toRightOf="@id/ta_br"
app:layout_constraintTop_toTopOf="@id/bottom_border"
app:layout_constraintBottom_toBottomOf="@id/bottom_border" />
<ImageView
android:id="@+id/ta_br"
style="@style/ta_img"
app:layout_constraintLeft_toLeftOf="@id/ta_bc"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@id/bottom_border"
app:layout_constraintBottom_toBottomOf="@id/bottom_border" />
<dimen name="border_margin">40dp</dimen>
sw720dp 10.1” tablet 1280x800 mdpi
sw600dp 7.0” tablet 1024x600 mdpi
sw480dp 5.4” 480x854 mdpi
sw480dp 5.1” 480x800 mdpi
xxhdpi 5.5" 1080x1920 xxhdpi
xxhdpi 5.5" 1440x2560 xxhdpi
xhdpi 4.7” 1280x720 xhdpi
xhdpi 4.65” 720x1280 xhdpi
hdpi 4.0” 480x800 hdpi
hdpi 3.7” 480x854 hdpi
mdpi 3.2” 320x480 mdpi
ldpi 3.4” 240x432 ldpi
ldpi 3.3” 240x400 ldpi
ldpi 2.7” 240x320 ldpi