2012-07-11 54 views
2

我有两个用于测试的android设备。一个是480x320分辨率,另一个是800x480。我在layout-normal和layout目录中定义了不同的布局。我也试过用layout-hdpi,layout-mdpi等不同的组合。知道运行时使用的布局

有没有一种方法可以从某个地方的日志中知道某个设备所属的布局类别只是为了调试目的。我想知道在运行时使用哪个目录的布局文件。如果没有,那么有人可以告诉我两种设备的布局目录的正确组合,具有前述的分辨率。

在此先感谢。

+0

请尝试[DisplayMetrics](http://developer.android.com/reference/android/util/DisplayMetrics.html)。 – Jens 2012-07-11 21:43:46

+0

了解高度和宽度? – dharmin007 2012-07-11 21:50:47

+1

不,“DisplayMetrics#density”将允许您确定它是否是MDPI设备(例如'DisplayMetrics#density == DisplayMetrics#MEDIUM_DENSITY')。 – Jens 2012-07-12 06:34:25

回答

8

要在运行期间使用哪种布局(从layout-ldpi,layout-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 
} 
1

这里是@ Angelo的延伸答案可能取决于你如何使用你的元素:在每个文件中,如果你有相同的元素,你不需要操作,你可以给它一个不同的ID为你定义的每个布局(而不是标记我T)。

例如,假设我不需要操纵基本的线性布局,我只需要操纵里面的视图。

这里是我的华电国际布局:

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

这里有一个MDPI布局:

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

这里是我的代码决定哪些布局是:

if (findViewById(R.id.layout-hdpi) != null) { 
    //we are in hdpi layout 
} else if (findViewById(R.id.layout-mdpi) != null) { 
    //we are in mdpi layout 
} 

的想法只有你为这个项目定义的id在你的不同文件中只有一个实际存在,并且其中的一个一个是在实际加载的布局中。需要注意的是,如果您以后确实需要操作该项目,则此方法会导致大量额外的工作,可能并不理想。您不希望在EditText等项目上使用此技术,因为您必须检查您所处的布局,以决定使用哪个ID来获取该编辑文本。例如