2015-04-22 34 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

好吧我会做一个编辑请等待 –

相关问题