2012-07-11 36 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來獲取該編輯文本。例如