2017-04-03 93 views
4

我应该创建不同的布局文件夹来存储我的xml布局文件。为了支持我的应用程序在不同的屏幕尺寸我应该为不同的android手机制作不同的布局(xml文件)

我开发了一个应用程序,添加drawable时会自动创建像xdpi ldpi和更多不同的大小,但不自动创建布局xml文件以支持不同的屏幕大小。我应该这样做吗?并且我还会使用支持屏幕标签来编辑清单文件以支持不同的尺寸。这是全部?它也会支持我的风景或肖像模式。请确认我。我是新来堆栈和Android开发。

编辑: 相信在不同的文件夹不同的布局文件..也只是用代码显示对方只用文件夹名称变更的广告副本

res/layout/my_layout.xml    // layout for normal screen size ("default") 
res/layout-small/my_layout.xml  // layout for small screen size 
res/layout-large/my_layout.xml  // layout for large screen size 
res/layout-xlarge/my_layout.xml  // layout for extra large screen size 
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation 

这是我的布局xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    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" 
    android:id="@+id/content_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 

    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:showIn="@layout/app_bar_main" 
    tools:context="com.example.root.meeransunday.MainActivity"> 


    <Button 
     android:id="@+id/button1" 
     android:layout_width="200dp" 
     android:layout_height="90dp" 
     android:text="Send Mobile" 
     android:drawableLeft="@mipmap/sms" 
     android:layout_alignParentBottom="true" 
     android:layout_marginRight="-1dp" 
     android:layout_marginLeft="-3dp" 
     android:layout_marginBottom="-4dp" 
     android:onClick="message"/> 
    <Button 
     android:id="@+id/button2" 
     android:layout_width="200dp" 
     android:layout_height="90dp" 
     android:text="QR Code" 
     android:drawableLeft="@mipmap/qr" 
     android:layout_marginLeft="190dp" 
     android:layout_marginRight="-20dp" 
     android:layout_marginBottom="-4dp" 
     android:layout_alignParentBottom="true" 
     android:onClick="scan" 
     /> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="30dp" 
    android:layout_centerHorizontal="true" 
    android:text="  My Account Balance" 
    android:textColor="#0D47A1" 
    /> 
    <TextView 
     android:text="PKR 1527.87" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="40dp" 
     android:layout_centerHorizontal="true" 
      android:drawableLeft="@mipmap/money" 
     android:textSize="35sp" 
     android:id="@+id/textView2" 

     /> 
</RelativeLayout> 

清单文件:

<supports-screens android:smallScreens="true" 
      android:normalScreens="true" 
      android:largeScreens="true" 
      android:xlargeScreens="true" 
      android:anyDensity="true" 
      android:resizeable="true"/> 

,但它不能在4我的工作nch屏幕。

+0

请检查该链接https://developer.android.com/guide/practices/screens_support.html –

+0

你检查'PercentRelativeLayout'? –

回答

2

如果布局相同,则不需要创建多个布局文件,您应该使用不同的维度文件来调整元素的大小。

如果您希望您的应用在hdpi设备上看起来与在xxxhdpi上看起来不同,或者如果您有移动设备和平板电脑版本的屏幕,则应使用多个布局文件。

+0

我有一个5英寸的RIVO PHANTOM PZ15屏幕(开发移动)和三星GALAXY J1的4英寸屏幕(测试屏幕)我的应用程序不适合在4英寸屏幕上显示。 。 该怎么办? –

+1

屏幕尺寸和屏幕密度是不同的东西。可能使用不同的维度文件,并且为较小的设备缩小视图是一种好方法。或者,如果您想从您的布局中移除元素以适合它,请使用不同的布局文件。 –

+0

所以@adrian科曼你可以请通过链接学习不同的Dimens在android中制作。 ?? –

3

这应该是有帮助的:https://developer.android.com/guide/practices/screens_support.html

无论如何,我认为你应该为活动使用最多2个XML布局(1水平来看,1俯视图)。

+1

这里是我说的:) https://developer.android.com/training/multiscreen/screensizes.html – 2017-04-03 07:49:26

+0

xml中应该有什么不同?像什么Android标签给我们改变方向?这也会支持所有的屏幕尺寸?像4 ich和5英寸..目前它不是 –

1

实际上为不同的android设备创建不同的xml布局取决于需求。对于所有类型的设备只有一个xml布局以避免冗余代码总是很好,但是我们可以为所有类型的设备创建xml,android具有该功能。

+0

如何让单个(一个)布局xml文件支持不同的android屏幕大小。像4英寸和5英寸? –

+0

您只能在android项目的布局目录中定义一个正常的.xml格式的xml布局。通过为布局的每个小部件应用适当的高度和宽度,您可以使用单个xml。 –

+0

请参阅对于这些类型的问题,最好为小型设备使用单独的.xml。您可以根据小型设备编辑您的高度和宽度。 –

0

没有必要设计所有的布局,如果你正在为手机和平板电脑制作应用程序,那么你可以设计2个布局。否则,仅设计这些 1.肖像 2.景观

+1

这两个布局layout_portrait.xml和layout_landlscape.xml是否足以支持所有屏幕尺寸?像4英寸和5英寸的屏幕尺寸? –

+1

是的,它会支持所有手机。它会根据屏幕尺寸自动调整。 –

+0

请参阅我的布局文件(我添加)作为编辑@abhishek Kumar它不起作用 –

相关问题