2016-05-15 134 views
0

我已经在布局文件中创建了布局并定义了以下XmlDP在不同的屏幕上显示不同的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.kirmani.myapplication.MainActivity" 
    android:orientation="vertical" 
    > 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="100dp" 
     android:background="@color/colorPrimary" 
     /> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="150dp" 
     android:background="@color/colorPrimaryDark" 
     /> 

    <ImageView 
     android:layout_width="405dp" 
     android:layout_height="260dp" 
     android:background="@color/colorAccent" 
     /> 

</LinearLayout> 

这是填充我的整个屏幕在Nexus 4屏幕完美。但是当我预览所有的屏幕时,它在一些屏幕上显得非常奇怪。

我正在使用DP,它应该保持相同的display根据所有屏幕,但它不是那样工作。请指导我..

enter image description here

回答

1

你的第三个imageview的宽度应该是匹配的父母。像这样

<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/colorAccent" 
    /> 

代替

<ImageView 
    android:layout_width="405dp" 
    android:layout_height="260dp" 
    android:background="@color/colorAccent" 
    /> 
+0

谢谢,这符合所有设备的宽度,但高度仍然是一个问题。在Nexus One中它会填满整个屏幕,但在其他屏幕中显示空间 - – Kirmani88

+0

您可以在第3个图像视图中使用您的身高匹配父母也 – Masum

2

其实DP装置,能根据所有屏幕相同的显示。这是因为每个设备都不应该在dp中具有相同的宽度。有些设备有480dp,他们中的一些360dp等

如果你想你的形象适合您的宽度,你必须使用

<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="260dp" 
    android:background="@color/colorAccent" 
    /> 

不使用固定的DP。

编辑:

如果要高度填满,使用此:

<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/colorAccent" 
    /> 
+0

感谢,这符合所有设备的宽度,但高度仍是个问题。在'Nexus One'中它填满了整个屏幕,但是在其他屏幕中它的末尾显示空间 – Kirmani88

+0

@ Kirmani88更新了我的答案 –

1

我们在这里讨论的Android ABC。你需要了解它是如何工作的,而不是寻找具体问题的具体答案,as I already recommend you here

每个设备在DP中都有一个特定尺寸。 Nexus 4的尺寸为384x640,但是这些尺寸是您图片中唯一的。请注意,这不是“在某些屏幕上看起来很奇怪”,实际上在所有屏幕上都不是384x640。例如,图像中的Nexus One(533dp x 320dp)没有像预期的那样工作,但更难以实现,因为哪里出错是超出了您的看法,但视图比屏幕更大。与Nexus S相同。

如果您尝试从边到边(所有屏幕宽度)获取视图,请使用match_parent。不要在DP中指定大小。

如果您尝试水平或垂直分布视图,请使用特定的ViewGroup和/或权重,或者您可以使用Percent Support Library来执行诸如thisthis之类的操作。 Take a look at this tutorial for more

无论如何,在这里你有你的副本&粘贴代码。

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="@color/colorPrimary" /> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1.5" 
     android:background="@color/colorPrimaryDark"/> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="2.6" 
     android:background="@color/colorAccent"/> 

</LinearLayout> 
+0

再次感谢:)您正在使用'height = 0'并使用'weight'它增加了“高度”,你能否详细说明“重量和高度”之间的差异。与'weight'一样,我可以添加任意数量的'Views',但是'Height'只添加一定数量的Views,并开始在小屏幕中隐藏一些'views'。 – Kirmani88

+0

http://developer.android.com/intl/es/guide/topics/ui/layout/linear.html – Sotti

+1

只要去阅读和做一些学习。不要使用StackOverflow进行学习,只是在阅读并尝试了几次后才解决问题,但仍然失败。 – Sotti