0

我正在嘗試解決佈局問題 - 我在Moto G上測試了這個res/layout/my.xml,這很好,但是當我在Nexus S上測試它時按鈕太大了。我嘗試res/layout-xhdpi/my.xml(因爲Moto G是xhdpi,而Nexus只是hdpi),但總是其中一個佈局在兩個上都膨脹,從來沒有不同(對於我現在使用的xhdpi,現在使用帶文本的按鈕,所以我可以即時看到,如果它繼續):Nexus S和Moto G - 不能區分佈局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:ads="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/LinearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#FFFFFF" 
    android:orientation="vertical" 
    tools:context=".MainActivity" > 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" > 

     <ImageView 
      android:id="@+id/imageView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:cropToPadding="true" 
      android:src="@drawable/pil2" /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignLeft="@+id/button1" 
      android:layout_below="@+id/button1" 
      android:height="90dp" 
      android:minWidth="150dp" /> 

     <Button 
      android:id="@+id/button3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignLeft="@+id/button2" 
      android:layout_below="@+id/button2" 
      android:height="80dp" 
      android:minWidth="150dp" /> 

     <Button 
      android:id="@+id/button4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignLeft="@+id/button1" 
      android:layout_alignParentTop="true" 
      android:height="130dp" 
      android:minWidth="150dp" /> 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/button4" 
      android:layout_centerHorizontal="true" 
      android:height="90dp" 
      android:minWidth="150dp" /> 

    </RelativeLayout> 

    <com.google.android.gms.ads.AdView 
     android:id="@+id/adView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     ads:adUnitId="xxxx" 
     ads:adSize="BANNER" /> 

</LinearLayout> 
+0

在可繪製資源以外的任何地方使用密度資源集限定符(例如'-hdpi')是一種嚴重的代碼異味,因爲它幾乎可以確保你不會得到你想要的東西。除此之外,使用層次結構視圖來確定您的佈局規則中的哪些內容會導致您的困難。 – CommonsWare 2014-10-05 12:06:42

+0

這就是它在Nexus S上的樣子:http://oi58.tinypic.com/oko8l1.jpg這就是它在Nexus 5上的樣子:http://oi59.tinypic.com/1zlgjud.jpg當然按鈕是在使它們正常工作之後,瑪德是透明的。從頂部的第一個按鈕沒有功能 - 只是佔位符,而不是margingTop。我一直在重新考慮自己的用戶界面,但無法讓這個東西變得牢固,所以任何設備都能正常工作。 – 2014-10-05 12:19:13

+0

這似乎是你試圖使該PNG/JPEG /任何背景可點擊的「按鈕」。你的方法不可靠,因爲按鈕與整體背景無關。 – CommonsWare 2014-10-05 12:26:32

回答

1

在價值觀夾有dimen.xml文件,並且有3個值的文件夾(值,值-V11,價值觀-V14)。如果你不這樣做在這些文件夾中有dimens.xml文件,那麼你可以爲它們創建dimens.xml文件,所以你可以給不同的設備不同的按鈕寬度和高度不同。像這樣: - 例如: - 對於值文件夾。

<dimen name="button_width">16dp</dimen> 
<dimen name="button_height">16dp</dimen> 

對於值-V11

<dimen name="button_width">26dp</dimen> 
<dimen name="button_height">26dp</dimen> 

對於價值觀V14現在

<dimen name="button_width">36dp</dimen> 
<dimen name="button_height">66dp</dimen> 

在主XML

android:layout_width="@+dimen\button_width" 
android:layout_height="@+dimen\button_height" 

我希望您的問題將解決。

+0

我認爲它可以解決這兩個設備的問題,但是我現在怎麼知道它是否可以在其他設備上正常工作?它是更多與API版本相關的問題還是面向設備? – 2014-10-05 08:30:51

+0

不,它會解決您的問題,所有類型的設備,如hdpi設備,7英寸平板電腦和10英寸平板電腦也...... – 2014-10-06 11:49:28