为什么我的图形有时使用相对布局?

时间:2011-06-04 14:17:07

标签: android android-layout android-xml

我一直有一个对我来说非常奇怪的问题。我使用的图形有时是居中的,原因是我无法理解,但仅限于某些显示器。顶部的图形是坏的例子,底部的图形是我想要的。它似乎不依赖于Android版本,Android 1.6上都复制了好的和坏的,并且在2.1上复制了坏处。它似乎只是尺寸依赖...

Bad version

Good level layout

相关的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>

正如你所看到的,我已经清楚地将图像的重力设置为左,告诉它与Parent左对齐,但由于某种未知的原因,它想要对齐中心。

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

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

在定义布局时仅使用@+id。否则使用@id

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

在布置layout_alignParentTop中的绝对项目时,您还可以使用layout_alignParentLeftlayout_alignParentRightlayout_alignParentBottomRelativeLayout。你肯定应该这样做。

答案 2 :(得分: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>

关键位是行android:layout_toLeftOf="@id/number_select",强制图像与textView不重叠。左边并不完美,但它们至少不重叠,所以我现在说的还不错。