我有两个Android设备要测试。一个分辨率为480x320,另一个为800x480。我在layout normal和layout目录中定义了不同的布局。我还尝试了布局hdpi,布局mdpi等不同的组合。
有没有一种方法可以从某个地方的日志中知道某个设备属于哪一种布局类别,仅用于调试目的。我想知道运行时从哪个目录使用的布局文件。如果没有,那么有人可以告诉我正确的布局目录组合为两个设备与上述的解决方案。
提前谢谢。

最佳答案

查找运行时使用的布局(从layout-ldpilayout-mdpi文件夹等…)。可以在布局中使用“标记”属性。例如,假设您为不同的屏幕定义了两种布局,一种在layout-mdpi文件夹中,另一种在layout-hdpi文件夹中。像这样的:

<?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>

要检查运行时使用的布局,可以使用以下方法:
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
}

关于android - 知道运行时使用的布局,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11441864/

10-14 15:37
查看更多