在android中设计布局

时间:2016-09-24 12:40:57

标签: android android-layout android-fragments

我想设计一个在不同分辨率的android手机中工作得很好的布局,例如给定的代码是布局代码我如何管理这个,所以它在各种不同分辨率的Android手机中都很好看。(分辨率就像480 * 800 768 * 1024等)任何人都可以帮助我

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/working_us"
        android:layout_gravity="center_horizontal"
        android:textColor="@color/heading"
        android:textSize="20sp"/>



    <TextView
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:text="@string/working_text"
        android:layout_gravity="center_horizontal"
        android:gravity="center"
        android:textColor="@color/text"
        android:textSize="12sp"/>


    <Button
        android:id="@+id/online_p"
        android:background="@drawable/rectangle"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:text="VISIT US ONLINE"
        android:textColor="#51bdd4"
        android:layout_height="wrap_content"
        android:layout_marginRight="20dp"
        android:layout_marginLeft="20dp"
        android:gravity="center"
        />


    <Button
        android:id="@+id/download_app"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="21dp"
        android:padding="20dp"
        android:layout_marginBottom="10dp"
        android:layout_gravity="bottom|center_horizontal"
        android:text="DOWNLOAD THE APP"
        android:textColor="#fff"
        android:background="@drawable/col" />
</LinearLayout>

3 个答案:

答案 0 :(得分:1)

您可以在res中创建不同的布局文件夹以获得小分辨率/大分辨率等,并将相同的副本放入不同的xml文件值

res/layout/main_activity.xml           # For handsets (smaller than 600dp available width)
res/layout-sw600dp/main_activity.xml   # For 7” tablets (600dp wide and bigger)
res/layout-sw720dp/main_activity.xml   # For 10” tablets (720dp wide and bigger)
res/layout/main_activity.xml         # For handsets (smaller than 600dp available width)
res/layout-w600dp/main_activity.xml  # Multi-pane (any screen with 600dp available width or more)

有关更多官方详情,请参阅this link

如果要限制以下策略可以使用

如果您的应用仅适用于最小可用宽度为600dp的平板电脑型设备:

<manifest ... >
    <supports-screens android:requiresSmallestWidthDp="600" />
    ...
</manifest>

或另一种替代方式是PercentRelativeLayout

浏览this链接获取演示代码。

答案 1 :(得分:0)

从您对该问题的评论中可以明显看出,在许多小分辨率手机中,布局看起来不太好,您可以使用不同的布局来支持小分辨率手机。以下是步骤:

  1. 您必须externalize所有维度(您不得在xml布局文件中对其进行硬编码)。
  2. 针对不同的屏幕密度存储桶(ldpi,mdpi,hdpi,xhdpi,xxhdpi等,如果需要,为xml文件创建单独的维度)
  3. 您可以将所有尺寸乘以一个系数,以获得不同屏幕密度的尺寸。
  4. 将所有维度与一个因子相乘会很繁琐,因此,您可以尝试my github repo,在输入因子值和项目位置后生成相应的维度文件内容。以下是参考代码:

    package com.company;
    
    import java.io.*;
    import java.util.Scanner;
    
    public class Main {
    
    
        /**
         * You can change your factors here. The current factors are just for the demo
         */
        private static final double LDPI_FACTOR = 0.375;
        private static final double MDPI_FACTOR = 0.5;
        private static final double HDPI_FACTOR = 0.75;
        private static final double XHDPI_FACTOR = 1.0;
        private static final double XXHDPI_FACTOR = 1.5;
        private static final double XXXHDPI_FACTOR = 2.0;
    
        private static double factor;
    
        public static void main(String[] args) throws IOException {
    
    
            Scanner in = new Scanner(System.in);
            System.out.println("Enter the location of the project/module");
            String projectPath = in.nextLine();
    
            System.out.println("Which of the following dimension file do you want?\n1. ldpi \n2. mdpi \n3. hdpi \n4. xhdpi \n5. xxhdpi \n6. xxxhdpi");
    
            int dimenType = in.nextInt();
    
            switch (dimenType) {
                case 1: factor = LDPI_FACTOR;
                    break;
                case 2: factor = MDPI_FACTOR;
                    break;
                case 3: factor = HDPI_FACTOR;
                    break;
                case 4: factor = XHDPI_FACTOR;
                    break;
                case 5: factor = XXHDPI_FACTOR;
                    break;
                case 6: factor = XXXHDPI_FACTOR;
                    break;
                default:
                    factor = 1.0;
            }
    
            //full path = "/home/akeshwar/android-sat-bothIncluded-notintegrated/code/tpr-5-5-9/princetonReview/src/main/res/values/dimens.xml"
            //location of the project or module = "/home/akeshwar/android-sat-bothIncluded-notintegrated/code/tpr-5-5-9/princetonReview/"
    
    
            /**
             * In case there is some I/O exception with the file, you can directly copy-paste the full path to the file here:
             */
            String fullPath = projectPath + "/src/main/res/values/dimens.xml";
    
            FileInputStream fstream = new FileInputStream(fullPath);
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
    
            String strLine;
    
            while ((strLine = br.readLine()) != null)   {
                modifyLine(strLine);
            }
            br.close();
    
        }
    
        private static void modifyLine(String line) {
    
            /**
             * Well, this is how I'm detecting if the line has some dimension value or not.
             */
            if(line.contains("p</")) {
                int endIndex = line.indexOf("p</");
    
                //since indexOf returns the first instance of the occurring string. And, the actual dimension would follow after the first ">" in the screen
                int begIndex = line.indexOf(">");
    
                String prefix = line.substring(0, begIndex+1);
                String root = line.substring(begIndex+1, endIndex-1);
                String suffix = line.substring(endIndex-1,line.length());
    
    
                /**
                 * Now, we have the root. We can use it to create different dimensions. Root is simply the dimension number.
                 */
    
                double dimens = Double.parseDouble(root);
                dimens = dimens*factor*1000;
                dimens = (double)((int)dimens);
                dimens = dimens/1000;
                root = dimens + "";
    
                System.out.println(prefix + " " +  root + " " + suffix );
    
            }
    
            System.out.println(line);
        }
    } 
    

答案 2 :(得分:0)

如果你想保持像素到像素的一致性,你需要在不同的值文件夹中维护尺寸文件,例如值-ldpi&gt;&gt;&gt; dimens.xml,values-mdpi&gt;&gt;&gt; dimens.xml,values- hdpi&gt;&gt;&gt; dimens.xml,values-xhdpi&gt;&gt;&gt; dimens.xml,values-xxhdpi&gt;&gt;&gt; dimens.xml,values-xxxhdpi&gt;&gt;&gt; dimens.xml并添加所需的dp或者根据分辨率的px值,你必须在所有文件夹中维护所有维度值的唯一名称,如10dp。