如何使android布局可扩展为不同的屏幕尺寸?

时间:2015-09-23 22:08:23

标签: android android-layout android-activity android-screen-support

我发现有很多问题要求支持不同的屏幕尺寸,屏幕尺寸从Tabletphone等不等。我正在编写我的第一个android应用程序我正在看不同手机的布局,所有这些都不是相对可扩展的屏幕。

我在这里粘贴了一个小RelativeLayout代码,以了解如何在较少的更改中使其适当扩展。我也遵循android官方文档并学习了两件事

  1. 不同屏幕的可绘制图像
  2. 使用sp提及text size,其他人使用dp
  3. 但它没有帮助我

    代码

    <?xml version="1.0" encoding="utf-8"?>
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffd5d6d6">
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="48dp"
        android:background="#ffffffff"
        android:elevation="@dimen/abc_action_bar_default_height_material">
    
        <ImageView
            android:id="@+id/back"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="16dp"
            android:src="@drawable/ic_back" />
    
        <TextView
            android:id="@+id/profileDesc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="Test"
            android:textColor="#ff3c3f41"
            android:textSize="15dp" />
    
        <ImageView
            android:id="@+id/flag"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="false"
            android:layout_alignParentStart="false"
            android:layout_alignWithParentIfMissing="false"
            android:layout_centerVertical="true"
            android:layout_marginRight="16dp"
            android:src="@drawable/temp_ic_share" />
    
    </RelativeLayout>
    
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="52dp">
    
        <TextView
            android:id="@+id/roommate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="false"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="40dp"
            android:text="Who are you?"
            android:textColor="#ff3c3f41"
            android:textSize="18dp" />
    
    
        <Button
            android:id="@+id/foodie"
            style="@style/Widget.AppCompat.ButtonBar"
            android:layout_width="60dp"
            android:layout_height="38dp"
            android:layout_alignParentStart="false"
            android:layout_marginLeft="56dp"
            android:layout_marginTop="80dp"
            android:background="#fa6425"
            android:text="Foodie"
            android:textColor="#fff4f4f4"
            android:textSize="12dp" />
    
        <Button
            android:id="@+id/button4"
            style="@style/Widget.AppCompat.ButtonBar"
            android:layout_width="80dp"
            android:layout_height="38dp"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="80dp"
            android:layout_toEndOf="@+id/foodie"
            android:background="#fa6425"
            android:text="Beach bum"
            android:textColor="#fff4f4f4"
            android:textSize="12dp" />
    
        <Button
            android:id="@+id/button5"
            style="@style/Widget.AppCompat.ButtonBar"
            android:layout_width="80dp"
            android:layout_height="38dp"
            android:layout_alignTop="@+id/button4"
            android:layout_marginLeft="15dp"
            android:layout_toEndOf="@+id/button4"
            android:background="#fa6425"
            android:text="Tree hugger"
            android:textColor="#fff4f4f4"
            android:textSize="12dp" />
        </RelativeLayout>
    
     <Button
        android:id="@+id/button"
        style="@style/Widget.AppCompat.ButtonBar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:background="#fa6425"
        android:text="Send"
        android:textColor="#fff4f4f4" />
    
    </RelativeLayout>
    

    正如您所看到的,我在一个主Parallel中加入了两个RelativeLayout RelativeLayout。这里有三种不同大小的观点:

    nexus one

    enter image description here

    nexus 5

    enter image description here

    nexus 6

    enter image description here

    最佳对齐位于Nexus 5屏幕。我是否需要创建不同的布局文件夹,即hdpi, mdpi等?如果我没有错,那么我就不需要了,因为我的应用程序只能在手机设备上运行(仅限纵向模式)。

    如何制作可扩展的屏幕布局。任何帮助都会很明显:​​)

3 个答案:

答案 0 :(得分:1)

如果你想强制所有视图在屏幕上具有相同的比例,唯一的方法是在计算出所需像素和屏幕密度之间的比率后动态地给它一个大小,这不是真正的解决方案,什么您需要了解的是,在处理各种屏幕尺寸时,每个人都假设行为不同,平板电脑屏幕预计会更大,因此屏幕上显示的元素空间更大,手机屏幕更小,元素应该更少在屏幕上显示,您需要调整视图以支持所有这些屏幕,正确的方法是为平板电脑和手机编写不同的布局,甚至通过屏幕dpi编写不同的布局。

答案 1 :(得分:-1)

我很新,我使用的是Android Studio,你使用的是什么?首先,如果您总是希望活动处于纵向模式,请在清单中使用此代码,您必须将代码添加到您希望保持纵向模式的每个活动中。如果不这样做,并且您没有添加代码来处理屏幕方向的开关,则手机可能会崩溃。

 <activity
        android:name=".TitleActivity"
        android:label="@string/title_activity"
        android:screenOrientation="portrait" >
 </activity>

尝试线性布局,而不是相对视图。

您将拥有主要布局,以及一些嵌套的线性布局。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/example">



<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">




<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Title Here"
    android:id="@+id/Title"
    android:layout_gravity="top|center_horizontal"
    android:layout_marginTop="20dp"
    android:gravity="center_horizontal"
    android:layout_marginBottom="20dp" />



</LinearLayout>


<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginBottom="50dp">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_weight="1"
        android:layout_marginLeft="10dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button2"
        android:layout_weight="1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button3"
        android:layout_weight="1"
        android:layout_marginRight="10dp" />
</LinearLayout>



<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">


    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/test"
        android:layout_margin="50dp" />
</LinearLayout>

此布局有一个标题在三个按钮之上,然后是三个按钮,然后是下面的图像视图,它从3.7扩展到7.0。大于7,我会做另一个布局。

您需要在可绘制文件中使用名为test的图像。

答案 2 :(得分:-1)

使用此库可帮助您为所有设备布局设置响应式可扩展视图

https://github.com/intuit/sdp

我非常简单,只需更换即可。

android:layout_width="50dp"
android:layout_height="60dp"

用。

android:layout_width="50sdp"
android:layout_height="60sdp"

和textView

android:textSize="60ssp"
而不是。属

相关问题