2015-04-22 36 views
0

我有一個佈局,它有一個框架佈局,它將顯示圖像,並在該圖像上我想顯示屏幕頂部的兩個按鈕。在這兩個屏幕的背景上有一個透明的水平條,我將其設置爲Relative或LinearLayout的背景。如何集中中央和兩個按鈕頂部的佈局

然後我想把兩個按鈕放在它們之間,它們之間有一個很好的距離,我希望相對佈局的背景只是在按鈕的右邊/左邊之後結束。在其他情況下,我不希望酒吧填滿整個寬度。我想在這裏,我張貼在圖像中。檢查link爲更好地瞭解,我想要什麼。我已經做到了這一點,但這不像在某些設備上那麼靈活這看起來很奇怪。

如果我使用的重量和重量有些然後按鈕圖像的寬度拉伸,這看起來很奇怪,因爲我的圖像拉伸的寬度。我有方形圖像。

這裏就是我想:

<LinearLayout 
    android:id="@+id/LinearLayout1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/transparent_image" 
    android:layout_gravity="center_horizontal"> 

    <ImageButton 
     android:id="@+id/btn_gallery" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_weight="1" 
     android:background="@android:color/transparent" 
     android:src="@drawable/btn_gallery" 
     /> 
    <TextView 
     android:layout_width="20dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center"/> 

    <ImageButton 
     android:id="@+id/btn_share" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_weight="1" 
     android:background="@android:color/transparent" 
     android:src="@drawable/btn_share" /> 
</LinearLayout> 
+0

只要把你的兩個按鈕''horizo​​ntal'和LinearLayout'設置重力'top'佈局。 – Piyush

+0

它沒有效果 –

+0

你是否實現了它?顯示我的文件。 – Piyush

回答

0

使用此代碼:

<?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="match_parent" 
android:orientation="vertical" > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_marginLeft="20dp" 
    android:layout_marginRight="20dp" 
    android:orientation="horizontal" 
    android:layout_height="60dp" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_weight="0.1" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_weight="0.1" 
     android:text="Button" /> 
</LinearLayout> 

</LinearLayout> 
+0

它走不要求@piyus古普塔 –

+0

你有沒有看到我的鏈接 –

+0

共享圖片再次檢查回答畫面的整個寬度。 – Piyush

2

設置layout_widthmatch_parent。 並將android:gravity ="center"添加到這兩個按鈕。

+0

這不是幫助 –

0

嘗試這樣做,以便根據您的參考更好地對齊。

<LinearLayout 
    android:id="@+id/LinearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:weightSum="2" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
android:background="@android:color/transparent" 
    android:layout_gravity="center_horizontal"> 

    <RelativeLayout 

    android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp" 
     android:layout_weight="1" 
     android:gravity="center" 
    > 
    <ImageButton 
     android:id="@+id/btn_gallery" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_weight="1" 

     android:layout_marginRight="10dp" 
     android:background="@android:color/transparent" 
     android:src="@drawable/ic_launcher" 
     /> 
    </RelativeLayout> 
<RelativeLayout 

    android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="5dp" 
     android:layout_weight="1" 
     android:gravity="center" 
    > 
    <ImageButton 
     android:id="@+id/btn_share" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_weight="1" 

     android:layout_marginLeft="10dp" 
     android:background="@android:color/transparent" 
     android:src="@drawable/ic_launcher" /> 
    </RelativeLayout> 
</LinearLayout> 
+0

這是不讓兩個按鈕之間的空間我想要一個適當的空間,他們之間它也是拉伸按鈕的圖像大小,我也不想 –

+0

酒吧的背景是整個寬度我不婉轉 –

+0

好吧我會做一個編輯請等待 –

相關問題