简体   繁体   English

Android - dimens.xml文件的别名?

[英]Android - Alias for dimens.xml file?

I'm trying to get my android app to support multiple screen sizes. 我正在尝试让我的Android应用程序支持多种屏幕尺寸。 I'm trying to keep my alternative layouts to a minimum by changing the dp values of the layouts using different dimens.xml files. 我正在尝试通过使用不同的dimens.xml文件更改布局的dp值来将我的替代布局保持在最低限度。

The crappy part is that I have 9 different folders that contain dimens files (values, values-sw320dp, values-sw480dp, values-sw600dp, and values-sw720dp for higher apis, values-small, values-normal, values-large, values-xlarge for older apis) and I hate copy pasting from my sw values folders to the old values folders over and over. 蹩脚的部分是我有9个不同的文件夹,包含尺寸文件(值,值-sw320dp,值-sw480dp,值-sw600dp,值-sw720dp用于更高的api,值 - 小,值 - 正常,值 - 大,值-xlarge for old apis)我讨厌将我的sw值文件夹中的粘贴复制到旧的值文件夹中。

I know how to create layout.xml file aliases like this shows , but does anyone know if you can do that for dimens files? 我知道如何创建layout.xml文件别名, 如下所示 ,但是有人知道你是否可以为尺寸文件做到这一点? I tried making multiple dimens files in my values.xml, but of course it doesn't work because then my two dimens files have properties with the same names, and that causes conflicts. 我尝试在我的values.xml中创建多个文件,但当然它不起作用,因为我的两个文件具有相同名称的属性,这会导致冲突。

Can I maybe name my dimens file in values-sw600dp dimens-large.xml and reference it in the values-large folder? 我可以在values-sw600dp dimens-large.xml命名我的dimension文件,并在values-large文件夹中引用它吗?

You can dynamically change size of layout components. 您可以动态更改布局组件的大小。 Here i am creating first xml layout according to 480*854 sized screen. 在这里,我根据480 * 854大小的屏幕创建第一个xml布局。 Below code automatic fits UI according to screen size. 下面的代码根据屏幕大小自动调整UI。

int width = 0;
int height = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);

    getTheDisplay();
    getUiComponents();
    adjustUIComponents();

}private void getTheDisplay() {

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    width = dm.widthPixels;
    height = dm.heightPixels;

}private void adjustUIComponents() {
    // myButton is button from layout xml file
    adjustUI(myButton);

}private void adjustUI(View v) {
    //myButton is in LinearLayout
       LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams)v.getLayoutParams();
    lp.width = calculateWidth(211);//211 is width of button in px given in xml
    lp.height = calculateHeight(80);//80 is height of button in px given in xml

    v.setLayoutParams(lp);
}private int calculateWidth(int w) {
    int wi = (w * width) / 480;
    return wi;
}

private int calculateHeight(int h) {
    int hi = (h * height) / 854;
return hi;
}

I ended up using Merlevede's suggestion, and adding a script to eclipse's build path. 我最终使用了Merlevede的建议,并在eclipse的构建路径中添加了一个脚本。

Specifically, select the android project, go to Project --> Properties --> Builders --> New 具体来说,选择android项目,转到Project - > Properties - > Builders - > New

Make a "Program" builder, then select a file that is natively executable on your file system. 创建“程序”构建器,然后选择在文件系统上本机可执行的文件。 Personally, I wrote a 3-line java app, jarred that, then wrote a batch file that just calls the java jar. 就个人而言,我写了一个3行java应用程序,震惊了,然后编写了一个只调用java jar的批处理文件。 The batch file is what I specified as the executable for the builder. 批处理文件是我指定的构建器的可执行文件。

Then I put that builder to the top of the build list, and voila, my files are being automatically copied! 然后我将该构建器放在构建列表的顶部,瞧,我的文件正在被自动复制!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM