【问题标题】:Nexus S and Moto G - can't differentiate layoutsNexus S 和 Moto G - 无法区分布局
【发布时间】:2014-10-04 10:47:58
【问题描述】:

我正在尝试解决布局问题 - 我在 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>

【问题讨论】:

  • 在除可绘制资源之外的任何东西上使用密度资源集限定符(例如,-hdpi)是一种严重的代码异味,因为几乎可以肯定你不会得到你想要的东西。除此之外,使用 Hierarchy View 来确定您的布局规则中是什么导致了您的困难。
  • 这是它在 Nexus S 上的样子:oi58.tinypic.com/oko8l1.jpg,这是它在 Nexus 5 上的样子:oi59.tinypic.com/1zlgjud.jpg 当然,按钮在使它们正常工作后变得透明。顶部的第一个按钮没有功能 - 只是占位符而不是 margingTop。我一直在重新考虑我的 UI,但无法让这个东西变得稳固,所以它在任何设备上都能正常工作。
  • 看来您正试图使显示在 PNG/JPEG/任何背景上的“按钮”可点击。您的方法不会可靠,因为按钮与整体背景无关。
  • 我想要的只是让背景 PNG 的按钮做出反应,以便飞行员真正发挥其功能(具有可点击的打开/关闭门区域)。那么你会推荐什么来代替呢?不会那么难 - 我见过更复杂的东西可以很好地工作。
  • "所以没有办法在 ImageView 上制作可点击区域?" ——Android 中没有任何东西可以在高水平上运行。毫无疑问,有些人已经解决了这个问题,但是否有可以应用的可重用解决方案是个大问题。在低级别,您正在响应触摸事件并在不规则按钮的顶部绘制一些不规则的叠加层以指示各种突出显示状态。

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


【解决方案1】:

在 Values 文件夹中有 dimen.xml 文件,并且有 3 个 values 文件夹(values、values-v11、values-v14)。如果这些文件夹中没有 dimen.xml 文件,则可以创建 dimen .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"

希望你的问题能解决。

【讨论】:

  • 我认为它可以解决这两个设备的问题,但是我现在怎么知道它是否可以在其他设备上正常工作?更多的是API版本相关的问题还是面向设备的问题?
  • 不,它可以解决所有类型设备的问题,例如 hdpi 设备、7 英寸平板电脑和 10 英寸平板电脑...
【解决方案2】:

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

最终,它可以在每台设备上完美运行,并且实施起来相当快。感谢所有回复!

【讨论】:

    猜你喜欢
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多