我有两个Android设备进行测试.一个是分辨率480x320,另一个是800x480.我在layout-normal和layout目录中定义了不同的布局.我也尝试过layout-hdpi,layout-mdpi等不同的组合.
有没有办法从某个地方的日志中知道设备在哪种布局类别中仅用于调试目的.我想知道在运行时使用哪个目录的布局文件.如果没有,那么有人可以告诉我两个设备的布局目录的正确组合与前面提到的分辨率.
提前致谢.
在运行时期间查找使用的布局(来自layout-ldpi,layout-mdpi文件夹等).您可以在布局上使用tag属性.例如,假设您为不同的屏幕定义了两个布局,一个在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>
Run Code Online (Sandbox Code Playgroud)
和:
<?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>
Run Code Online (Sandbox Code Playgroud)
要检查运行时使用的布局,可以使用以下内容:
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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2782 次 |
| 最近记录: |