2013-07-19 69 views
0

請看看下面的代碼如何將按鈕對齊到LinearLayout的最右角?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:background="#373734" 
    android:orientation="horizontal" > 

     <ImageView 
      android:id="@+id/backToLanguageSelectionButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:paddingBottom="5dp" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="15dp" 
      android:paddingTop="5dp" 
      android:src="@drawable/thunderbolt" /> 

     <ImageView 
      android:id="@+id/internetButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:paddingBottom="5dp" 
      android:paddingTop="5dp" 
      android:src="@drawable/globe_small_2" /> 

     <Button 
      android:id="@+id/giveUpButton" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="350dp" 
      android:paddingTop="5dp" 
      android:paddingBottom="5dp" 
      android:text="Button" /> 



</LinearLayout> 

這是我使用所有的Android活動的共同佈局代碼。問題在於按鈕,我希望它移動到最右邊的角落。保證金的東西似乎不能正常工作,因爲在不同的設備中,按鈕位於不同的地方。

如何將其移動到所有設備中顯示的最右側角落?

回答

3

我相信你可以添加一個視圖中的第二ImageView的和你的按鈕之間,並將其設置爲layout_width =」 0dp「,layout_weight =」1「。 並刪除按鈕的左邊距。

+0

好吧,這是超級棒!謝謝! –

+0

它的工作。這是如何工作的?如果我得到解釋,將會有所幫助。通過將0dp設置爲的寬度,它在按鈕之間創建了最大空間。怎麼樣 ???? – Pravin

+0

好吧,layout_weight顯然。 「這個屬性爲視圖指定了一個」重要性「的值,它應該佔用多少空間在屏幕上。」來自LinearLayout文檔。 – Nerkatel

1

你試過android:layout_gravity="right"

+0

剛剛做了,沒有效果:( –

+0

是卡在左邊的按鈕? – Blackbelt

+0

是啊!................. –

2

改爲用戶相對佈局。並將android:layout_alignParentRight =「true」應用於Button。

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_alignParentBottom="true" 
android:background="#373734" 
android:orientation="horizontal" > 

    <ImageView 
     android:id="@+id/backToLanguageSelectionButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingBottom="5dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="15dp" 
     android:paddingTop="5dp" 
     android:src="@drawable/social_share" /> 

    <ImageView 
     android:id="@+id/internetButton" 
     android:layout_toRightOf="@+id/backToLanguageSelectionButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingBottom="5dp" 
     android:paddingTop="5dp" 
     android:src="@drawable/social_share" /> 

    <Button 
     android:id="@+id/giveUpButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:paddingTop="5dp" 
     android:paddingBottom="5dp" 
     android:text="Button" /> 

+0

RelativeLayout佔據了整個屏幕!實際上,這是一個小的狀態欄類型佈局 –

+0

@Knight請參閱我的更新回答 – sirackh

+0

哇。謝謝。另一種方法。+1從我:) –

0

它可以與RelativeLayout的做

{<Button android:id="@+id/giveUpButton" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginLeft="350dp" android:paddingBottom="5dp" android:paddingTop="5dp" android:text="Button" android:layout_alignParentTop="true" android:layout_alignParentRight="true" />}

+0

此屬性是有效的,只有當RelativeLayout是父母... – Sajmon

0

使用RelativeLayout和將它的高度設置得像你想要的那樣小。這樣你屏幕的一小部分就會被填滿。當您使用RelativeLayout時,您可以使用android:layout_alignParentRight="true"將任何項目放在正確的位置。現在,如果按鈕縮放出現問題,則可以使用圖像並設置其android:scaleType="centerInside",而不是按鈕。 我遇到了同樣的問題,因爲我使用RelativeLayout來解析它。