2013-07-03 107 views
12

我有以下佈局:Android的相對佈局對齊父權

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/test_ll" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/background" 
android:orientation="vertical" > 

<RelativeLayout 
    android:id="@+id/main_layPanelCircle" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical|center_horizontal" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:layout_marginTop="15dp" 
    android:background="@drawable/main_panel_a" 
    android:orientation="vertical" 
    android:padding="10dp" > 

    <ImageView 
     android:id="@+id/main_imgBreachAlert" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="5dp" /> 

    <Button 
     android:id="@+id/main_btnMap" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:background="@null" 
     android:drawableTop="@drawable/icon_find" 
     android:text="@string/main_locate" 
     android:textColor="@android:color/white" 
     android:textSize="10sp" /> 

    <LinearLayout 
     android:id="@+id/main_llPanelCycleMiddle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/main_btnMap" 
     android:layout_centerInParent="true" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/main_btnLeft" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@null" 
      android:drawableTop="@drawable/icon_call" 
      android:text="@string/main_call" 
      android:textColor="@android:color/white" 
      android:textSize="10sp" 
      android:visibility="visible" /> 

     <Button 
      android:id="@+id/main_btnEmergency" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@null" 
      android:drawableTop="@drawable/icon_emergency" 
      android:scaleType="center" 
      android:text="@string/main_emergency" 
      android:textColor="@android:color/white" 
      android:textSize="9sp" 
      android:textStyle="bold" /> 

     <Button 
      android:id="@+id/main_btnRight" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@null" 
      android:drawableTop="@drawable/icon_store" 
      android:text="@string/main_store" 
      android:textColor="@android:color/white" 
      android:textSize="10sp" /> 
    </LinearLayout> 

    <Button 
     android:id="@+id/main_btnMore" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/main_llPanelCycleMiddle" 
     android:layout_centerHorizontal="true" 
     android:background="@null" 
     android:drawableTop="@drawable/icon_more" 
     android:text="@string/main_more" 
     android:textColor="@android:color/white" 
     android:textSize="10sp" /> 
</RelativeLayout> 

</LinearLayout> 

當我設置main_imgBreachAlert機器人:layout_alignParentLeft =「真」 eveerything是OK(左圖),但是當我將它設置爲正確的礦井相對佈局舒展(左圖):

http://i.imgur.com/uDJqF0k.jpg?1

請您指教,我怎麼能解決這個問題。

日Thnx

+0

你是否在同一時間將它與父母左右對齊?如果是這樣,那就迫使它填充父項,重載wrap_content(作爲兩個邪惡中的較小者,另一個選項是將佈局中的其他任何東西縮小到相同的大小)。 –

+0

你找到了你的問題的答案? Iam遇到完全相同的問題 – Snake

回答

26

嘗試android:layout_centerInParent="true",如果你想在你的佈局爲中心的圖像刪除alignParentLeft屬性。

如果您只想將它​​對齊到右側,請使用android:layout_alignParentRight="true"

<ImageView 
    android:id="@+id/main_imgBreachAlert" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_marginRight="5dp" /> 
+2

我想將右側的圖像設置爲不居中。 – Damir

+1

然後只使用android:alignParentRight =「true」並刪除alignParentLeft屬性。 – Flynn81

+2

如果我設置alignParentRight比它拉伸父控件所在的父項。 我認爲我的方法是錯誤的。我實際上想要在背景周圍設置佈局背景和換行佈局,並且將其他控件放在該佈局中,但它應該圍繞背景圖像保留 。如果這是有道理的。 – Damir