0

我一直遇到一个对我来说非常奇怪的问题。由于我无法理解的原因,我使用的图形有时会居中,但仅在某些显示器上。上图是一个坏例子,下图是我想要的。它似乎与 Android 版本无关,好的和坏的都在 Android 1.6 上复制,而坏的在 2.1 上复制。它似乎只取决于大小......

坏版本

良好的层次布局

相关的 XML 代码:

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
  android:layout_height="fill_parent" android:layout_width="fill_parent">
    <com.google.ads.AdView android:id="@+id/adView" 
        ads:loadAdOnCreate="true" ads:adSize="BANNER" 
        ads:adUnitId="@string/admob_unit_id" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true" ></com.google.ads.AdView>
    <pearsonartphoto.AJEG.number_pad android:id="@+id/numberpad" 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:layout_above="@id/adView" 
        android:layout_centerHorizontal="true"></pearsonartphoto.AJEG.number_pad>
    <pearsonartphoto.AJEG.Level_Score_bar android:id="@+id/Score_Bar" 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent"></pearsonartphoto.AJEG.Level_Score_bar>
    <ImageView android:id="@+id/logo_image" 
        android:layout_below="@id/Score_Bar" 
        android:layout_above="@id/numberpad" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/level_logo" 
        android:layout_alignParentLeft="true" 
        android:layout_centerInParent="false"
        android:gravity="left"></ImageView>
    <TextView android:id="@+id/number_select" 
        android:layout_above="@id/numberpad"
        android:layout_below="@id/Score_Bar"
        android:text="0" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        style="@style/numberSelect" 
        android:layout_centerHorizontal="true"
        android:gravity="center"></TextView>
    <pearsonartphoto.AJEG.number_viewer android:id="@+id/numberviewer" 
        android:layout_alignParentRight="true" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_below="@id/Score_Bar"></pearsonartphoto.AJEG.number_viewer>
</RelativeLayout>

如您所见,我已经清楚地将图像的重力设置为左,告诉它与父左对齐,但由于某些未知原因它想要对齐中心。

4

3 回答 3

2
Please try this code,, 

<?xml version="1.0" encoding="utf-8"?>

  <RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
       xmlns:ads = "http://schemas.android.com/apk/lib/com.google.ads"
       android:layout_width = "fill_parent"
       android:layout_height = "fill_parent">

   <RelativeLayout android:id = "@+id/1stRLayout" 
        android:layout_width = "fill_parent"
        android:layout_height = "wrap_content"
        android:layout_alignParentBottom = "true">

    <com.google.ads.AdView android:id = "@+id/adView" 
        ads:loadAdOnCreate = "true" 
        ads:adSize = "BANNER" 
        ads:adUnitId = "@string/admob_unit_id" 
        android:layout_width = "wrap_content" 
        android:layout_height = "wrap_content">
    </com.google.ads.AdView>
  </RelativeLayout>


  <RelativeLayout android:id = "@+id/2ndRLayout" 
        android:layout_width = "fill_parent"
        android:layout_height = "wrap_content"
        android:layout_above = "@id/1stRLayout"
        android:gravity = "center">

    <pearsonartphoto.AJEG.number_pad android:id = "@+id/numberpad" 
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content">
    </pearsonartphoto.AJEG.number_pad>  
  </RelativeLayout>

  <RelativeLayout android:id = "@+id/3rdRLayout"
        android:layout_width = "fill_parent"
        android:layout_height = "70dp"
        android:layout_above = "@id/2ndRLayout">

    <ImageView android:id = "@+id/logo_image" 
        android:layout_width = "wrap_content" 
        android:layout_height = "wrap_content" 
        android:src = "@drawable/level_logo" 
        android:layout_marginLeft = "8dp"
        android:layout_alignParentLeft = "true"
        android:layout_marginTop = "6dp">
    </ImageView>

    <TextView android:id = "@+id/number_select" 
        android:layout_width = "wrap_content" 
        android:layout_height = "wrap_content" 
        android:text = "0"  
        style = "@style/numberSelect" 
        android:layout_centerInParent = "true">
    </TextView>

    <pearsonartphoto.AJEG.number_viewer android:id = "@+id/numberviewer" 
        android:layout_width = "wrap_content" 
        android:layout_height = "wrap_content" 
        android:layout_marginRight = "8dp"
        android:layout_alignParentRight = "true"
        android:layout_marginTop = "6dp">
    </pearsonartphoto.AJEG.number_viewer>
  </RelativeLayout>

  <RelativeLayout android:id = "@+id/4thRLayout"
        android:layout_width = "fill_parent"
        android:layout_height = "wrap_content"
        android:layout_alignParentTop = "true">

    <pearsonartphoto.AJEG.Level_Score_bar android:id = "@+id/Score_Bar" 
        android:layout_height = "wrap_content" 
        android:layout_width = "fill_parent">
    </pearsonartphoto.AJEG.Level_Score_bar>
  </RelativeLayout>

</RelativeLayout>
于 2011-06-25T10:19:40.520 回答
0

根据您对 ImageView 布局的定义,layout_above="@+id/numberpad您可能会遇到问题,numberviewer如下所示。上面的代码应该可以解决这个问题。

@+id在定义布局时使用。其他使用@id

此外,当在 a 中定义布局位置时,RelativeLayout您应该考虑将相对于其他元素定位的元素放置在布局文件的底部。

您还可以使用layout_alignParentTop,layout_alignParentLeft或在. 在您的情况下,您绝对应该这样做。layout_alignParentRightlayout_alignParentBottomRelativeLayout

于 2011-06-04T14:25:42.400 回答
0

我设法通过一些诡计解决了这个问题,但出于不同的原因我不得不投入其中。然而,它可以用一块石头解决两个问题。

基本上,我的 ImageView 变成了这样:

<ImageView android:id="@+id/logo_image" 
    android:layout_below="@id/Score_Bar" 
    android:layout_above="@id/numberpad" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/level_logo" 
    android:layout_alignParentLeft="true" 
    android:layout_toLeftOf="@id/number_select" 
    android:layout_centerInParent="false"
    android:gravity="left"></ImageView>

关键位是 line android:layout_toLeftOf="@id/number_select",强制图像不与带有数字的 textView 重叠。它在左边并不完美,但它们至少不会重叠,所以我现在说的已经足够好了。

于 2011-06-05T05:45:02.800 回答