在运行时使用明知布局 [英] Knowing layout used at runtime

查看:139
本文介绍了在运行时使用明知布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有测试两款Android设备。一个是与小480x320的分辨率和其他是800×480。我定义布局正常和布局目录不同的布局。我还与布局,华电国际,布局于mdpi等不同组合的尝试。

I have two android devices for testing. One is with resolution 480x320 and other is with 800x480. I define different layouts in layout-normal and layout directories. I also tried it with layout-hdpi, layout-mdpi etc different combinations.

有没有办法从日志某处要知道在哪个布局类的设备正好落在用于调试目的。我想知道从哪个目录在运行时使用的布局文件。如果没有,那么可能有人告诉我,布局目录的正确组合以pre-提到解决两个设备。

Is there a way to know from the log of somewhere that in which layout category a device falls just for debugging purposes. I would like to know layout file from which directory is used at runtime. If not then could someone tell me the right combination of layout directories for two devices with the pre-mentioned resolution.

先谢谢了。

推荐答案

要找到它的布局(从布局LDPI 布局于mdpi 文件夹等)在运行期间使用。您可以使用标签属性上的布局。例如假设您已经定义了两个布局不同的屏幕,一个在布局于mdpi 文件夹和其他在布局华电国际文件夹中。事情是这样的:

To find which layout (from layout-ldpi, layout-mdpi folder etc...) is used during runtime. You can use the tag attribute on your layout. For example let's say you have defined two layouts for different screens, the one in layout-mdpi folder and the other in layout-hdpi folder. Something like this:

<?xml version="1.0" encoding="utf-8"?>
<!--Layout defined in layout-mdi folder-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MainLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:tag="mdpi"
    android:orientation="horizontal" >

    <!-- View and layouts definition-->
<!LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<!--Corresponding Layout defined in layout-hdi folder-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MainLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:tag="hdpi"
    android:orientation="horizontal" >

    <!-- View and layouts definition-->
<!LinearLayout>

要检查哪些布局在运行期间使用,就可以使用这样的:

To check which layout is used during runtime, you can use something like this:

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.MainLayout);
if(linearLayout.getTag() != null) {

   String screen_density = (String) linearLayout.getTag();
}

if(screen_density.equalsIgnoreCase("mdpi") {
   //layout in layout-mdpi folder is used
} else if(screen_density.equalsIgnoreCase("hdpi") {
   //layout in layout-hdpi folder is used
}

这篇关于在运行时使用明知布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆