【发布时间】: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