如何为android中的每个不同屏幕大小定义dimens.xml?
在Android中支持不同的屏幕大小(密度)时,重点通常是为每个可能的屏幕创建不同的布局。即如何为android中的每个不同屏幕大小定义dimens.xml?,android,android-layout,android-activity,android-screen-support,Android,Android Layout,Android Activity,Android Screen Support,在Android中支持不同的屏幕大小(密度)时,重点通常是为每个可能的屏幕创建不同的布局。即 ldpi mdpi hdpi xhdpi xxhdpi xxxhdpi 我为xhdpi屏幕设计了一个布局作为参考,并在dimens.xml中定义了维度。现在我想支持所有可能的屏幕大小。我该怎么做? 据我所知,我可以使用该工具为其他屏幕大小找出合适的dimens.xml,并将其添加到我的项目中。在我的情况下,这样做对吗 另一个问题,我是否只需要为上述屏幕维度创建dimens.xml?如果是,那么什么是
- ldpi
- mdpi
- hdpi
- xhdpi
- xxhdpi
- xxxhdpi
w820dp
?
谢谢你的帮助。我只需要支持手机(不支持平板电脑或其他设备)。您可以在 1) 值 2) 值hdpi 3) 值xhdpi 4) 值xxhdpi
并根据密度在相应文件夹中的
dimens.xml
中指定不同的大小。您必须为不同屏幕创建不同的值
文件夹,并根据密度放置dimens.xml
文件
1) values
2) values-hdpi (320x480 ,480x800)
3) values-large-hdpi (600x1024)
4) values-xlarge (720x1280 ,768x1280 ,800x1280 ,Nexus7 ,Nexus10)
5) values-sw480dp (5.1' WVGA screen)
6) values-xhdpi (Nexus4 , Galaxy Nexus)
您必须为不同的屏幕创建不同的值文件夹。 像 有关更多信息,请访问此处 由
您可以使用Android Studio插件,根据自定义比例因子为其他像素桶自动生成尺寸值。它仍处于测试阶段,请务必将遇到的任何问题/建议通知开发人员。我上传了一个简单的java程序,它将您的项目位置和所需的维度文件作为输入。基于此,它将在控制台中输出相应的维度文件。以下是它的链接: 以下是供参考的完整代码:
public class Main {
/**
* You can change your factors here. The current factors are in accordance with the official documentation.
*/
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);
}
}
公共类主{
/**
*您可以在此处更改系数。当前系数与官方文件一致。
*/
专用静态最终双LDPI_系数=0.375;
专用静态最终双MDPI_系数=0.5;
专用静态最终双HDPI_系数=0.75;
专用静态最终双XHDPI_系数=1.0;
专用静态最终双XXHDPI_系数=1.5;
专用静态最终双XXXHDPI_系数=2.0;
私人静态双重因素;
公共静态void main(字符串[]args)引发IOException{
扫描仪输入=新扫描仪(系统输入);
System.out.println(“输入项目/模块的位置”);
字符串projectPath=in.nextLine();
System.out.println(“您想要以下哪个维度文件?\n1.ldpi\n2.mdpi\n3.hdpi\n4.xhdpi\n5.xxhdpi\n6.xxxhdpi”);
int-dimenType=in.nextInt();
开关(尺寸类型){
情况1:系数=LDPI_系数;
打破
案例2:因子=MDPI_因子;
打破
案例3:系数=HDPI_系数;
打破
案例4:系数=XHDPI_系数;
打破
案例5:系数=XXHDPI_系数;
打破
案例6:系数=XXXHDPI_系数;
打破
违约:
系数=1.0;
}
//完整路径=“/home/akeshwar/android sat bothIncluded notintegrated/code/tpr-5-5-9/princetonReview/src/main/res/values/dimens.xml”
//项目或模块的位置=“/home/akeshwar/android sat bothIncluded notintegrated/code/tpr-5-5-9/princetonereview/”
/**
*如果文件存在某些I/O异常,您可以在此处直接复制粘贴文件的完整路径:
*/
字符串fullPath=projectPath+“/src/main/res/values/dimens.xml”;
FileInputStream fstream=新的FileInputStream(完整路径);
BufferedReader br=新的BufferedReader(新的InputStreamReader(fstream));
弦斯特林;
而((strLine=br.readLine())!=null){
改装线(斯特林);
}
br.close();
}
私有静态void modifyLine(字符串行){
/**
*这就是我检测直线是否有维度值的方法。
*/
如果(第行包含(“p”);
字符串前缀=行。子字符串(0,begIndex+1);
字符串根=行。子字符串(begIndex+1,endIndex-1);
字符串后缀=line.substring(endIndex-1,line.length());
/**
*现在,我们有了根。我们可以用它来创建不同的维度。根就是维度号。
*/
double dimens=double.parseDouble(根);
直径=直径*系数*1000;
双精度=(双精度)((整数)双精度);
直径=直径/1000;
根=dimens+“”;
System.out.println(前缀+根+后缀);
}
系统输出打印项次(行);
}
}
我们希望看到不同屏幕中所需视图大小的变化
我们需要为不同的屏幕创建不同的值文件夹,并根据屏幕密度放置dimens.xml文件
1) values
2) values-hdpi (320x480 ,480x800)
3) values-large-hdpi (600x1024)
4) values-xlarge (720x1280 ,768x1280 ,800x1280 ,Nexus7 ,Nexus10)
5) values-sw480dp (5.1' WVGA screen)
6) values-xhdpi (Nexus4 , Galaxy Nexus)
我使用了一个TextView,并观察了在中更改dimens.xml时所做的更改
不同的文件夹值
请遵循流程
normal-xhdpi\dimens.xml
当我们更改正常的-xhdpi\dimens.xml时,以下设备可以更改屏幕的大小
nexus 5X(5.2英寸*1080*1920:420dpi)
nexus 6P(5.7英寸*1440*2560:560dpi)
nexus 6(6.0英寸*1440*2560:560dpi)
nexus 5(5.0英寸,1080 1920:xxhdpi)
nexus 4(4.7英寸),768*1280:
<TextView
android:layout_width="@dimen/_50sdp"
android:layout_height="@dimen/_50sdp"
android:text="Hello World!" />
implementation 'com.intuit.ssp:ssp-android:1.0.5'
implementation 'com.intuit.sdp:sdp-android:1.0.5'
android:layout_marginTop="@dimen/_80sdp"