Android 布局操作栏按钮

发布于 2024-12-08 07:20:46 字数 1437 浏览 0 评论 0原文

抱歉这个有点无聊的问题,但我试图在“操作栏”上布局按钮,但使用“layout_alignRight”等遇到了麻烦。我想要实现的目标如下:

|[button1] _ _ _ 空白 _ _ _ [button2] [button3] [button4]|

我有一个relativelayout包含四个按钮应该放置的位置,并且我尝试了以下代码但无济于事:

<RelativeLayout android:id="@+id/actionBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#666666" >


    <Button android:id="@+id/button1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignLeft="@id/actionBar"
        android:background="@drawable/buttonImg" />


    <Button android:id="@+id/button4"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignRight="@id/actionBar"
        android:background="@drawable/buttonImg" />


    <Button android:id="@+id/button3"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_toLeftOf="@id/button2"
        android:background="@drawable/buttonImg" />


    <Button android:id="@+id/button2"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_toLeftOf="@id/button3"
        android:background="@drawable/buttonImg" />                 


</RelativeLayout>

按钮似乎总是被拉伸得很远,或者全部都堆积在彼此之上。有谁知道如何实现我正在寻找的间距?

谢谢!!!

Sorry for the somewhat boring question, but I am trying to lay out buttons on an "action bar" and am running into trouble using "layout_alignRight" and the like. What I want to achieve is the following:

|[button1] _ _ _ empty space _ _ _ [button2] [button3] [button4]|

I have a RelativeLayout enclosing where the four buttons should sit, and I've tried the following code to no avail:

<RelativeLayout android:id="@+id/actionBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#666666" >


    <Button android:id="@+id/button1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignLeft="@id/actionBar"
        android:background="@drawable/buttonImg" />


    <Button android:id="@+id/button4"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignRight="@id/actionBar"
        android:background="@drawable/buttonImg" />


    <Button android:id="@+id/button3"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_toLeftOf="@id/button2"
        android:background="@drawable/buttonImg" />


    <Button android:id="@+id/button2"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_toLeftOf="@id/button3"
        android:background="@drawable/buttonImg" />                 


</RelativeLayout>

The buttons always seem to get stretched way out, or to all get piled up on top of each other. Does anyone know how to achieve the spacing I'm looking for?

Thanks!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霊感 2024-12-15 07:20:46

仅使用相对布局时,您必须锚定最左侧的按钮,然后是最右侧的按钮,然后添加与最右侧按钮左侧对齐的其他两个按钮:

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <Button android:id="@+id/b1" android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:text="Button1"></Button>
    <Button android:id="@+id/b4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:layout_alignParentRight="true"
      android:text="Button4"></Button>
    <Button android:id="@+id/b3"
      android:layout_width="wrap_content"      
      android:layout_height="wrap_content"
      android:layout_toLeftOf="@id/b4"
      android:text="Button3"></Button>
    <Button android:id="@+id/b2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_toLeftOf="@id/b3"
      android:text="Button2"></Button>
</RelativeLayout>

With using only relative layout, you have to anchor the left most button, then the rightmost button, and then add the two other buttons aligning left of the rightmost button:

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <Button android:id="@+id/b1" android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:text="Button1"></Button>
    <Button android:id="@+id/b4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:layout_alignParentRight="true"
      android:text="Button4"></Button>
    <Button android:id="@+id/b3"
      android:layout_width="wrap_content"      
      android:layout_height="wrap_content"
      android:layout_toLeftOf="@id/b4"
      android:text="Button3"></Button>
    <Button android:id="@+id/b2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_toLeftOf="@id/b3"
      android:text="Button2"></Button>
</RelativeLayout>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文