Nexus S和Moto G - 无法区分布局

时间:2014-10-04 10:47:58

标签: android xml android-layout android-button android-relativelayout

我正在尝试解决布局问题 - 我在Moto G上测试了这个res / layout / my.xml并且很好,但是当我在Nexus S上测试它时按钮太大了。我试过res / layout-xhdpi / my.xml(因为Moto G是xhdpi而Nexus只是hdpi),但总是其中一个布局在两者上都膨胀,每个都没有不同(对于xhdpi我现在使用按钮带有文本所以我可以立即看到它是否继续):

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

2 个答案:

答案 0 :(得分:1)

在Values文件夹中有dimen.xml文件,并且有3个值文件夹(values,values-v11,values-v14)。如果你在这些文件夹中没有dimens.xml文件,那么你可以为它们创建dimens.xml文件,因此您可以为不同的设备提供不同的按钮宽度和高度。像这样:- 例如: - 对于值文件夹。

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

对于values-v11

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

对于values-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"

我希望你的问题能解决。

答案 1 :(得分:0)

好的,我选择了解决方案:http://blahti.wordpress.com/2012/06/26/images-with-clickable-areas/

最终,它可以在每台设备上完美运行,并且可以很快地实现。感谢所有回复!