【问题标题】:How to put an ImageButton above a centered ImageButton in a relative layout如何在相对布局中将 ImageButton 放在居中的 ImageButton 上方
【发布时间】:2016-01-25 22:14:12
【问题描述】:

我有一个RelativeLayout 居中我有一个ImageButton 居中。我想要另一个在第一个的右侧,但它显示在屏幕左侧,虽然与顶部对齐。

我使用了相对布局,因为我希望按钮彼此相对。 (这是错的吗?)

我总共有四个按钮。

#1 centered.
#2 to the right of #1
#3 above #2
#4 to the left of #3

像这样

.................
.               .
.       43      .
.       12      .
.               .
.................

#1 位于中间。目前它看起来像这样:

.................
.               .
.43             .
. 2     1       .
.               .
.................
  • 我做错了什么?

这是布局文件:

<RelativeLayout
android:id="@+id/board_id"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg480x800"
android:orientation="vertical"
android:padding="0dp" >

<Button
    android:id="@+id/game_choice_6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/game_choice_4" />

<Button
    android:id="@+id/game_choice_3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/game_choice_1" />

<Button
    android:id="@+id/game_choice_4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/game_choice_2" />

<Button
    android:id="@+id/game_choice_5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/game_choice_3" />

<Button
    android:id="@+id/game_choice_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@id/square_layout_id"
    android:layout_centerVertical="true" />

<Button
    android:id="@+id/game_choice_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@id/square_layout_id" />

<com.brdgms.android.SquareLayout
    android:id="@+id/square_layout_id"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="#00000000" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent" >

        <ImageView
            android:id="@+id/game_board"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="game Board"
            android:src="@drawable/game_board" />

        <com.brdgms.android.games.game.GameBoardView
            android:id="@+id/game_canvas"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <ImageButton
            android:id="@+id/button_game_player_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="@drawable/game_player_1_button" />

        <ImageButton
            android:id="@+id/button_game_player_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/button_game_player_1"
            android:layout_toRightOf="@+id/button_game_player_1"
            android:layout_toEndOf="@+id/button_game_player_1"
            android:src="@drawable/game_player_2_button" />

        <ImageButton
            android:id="@+id/button_game_player_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/button_game_player_2"
            android:layout_alignLeft="@+id/button_game_player_2"
            android:src="@drawable/game_player_3_button" />

        <ImageButton
            android:id="@+id/button_game_player_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/button_game_player_3"
            android:layout_toLeftOf="@+id/button_game_player_3"
            android:src="@drawable/game_player_4_button" />

    </RelativeLayout>

</com.brdgms.android.SquareLayout>

</RelativeLayout>

【问题讨论】:

    标签: android android-relativelayout


    【解决方案1】:

    为什么要使用相对布局?改用线性布局。使其水平并将线性布局上的“android:gravity”设置为居中。

    <?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:gravity="center"
              android:orientation="horizontal">
    
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/some_drawable"/>
    
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/some_drawable"/>
    
    </LinearLayout>
    

    【讨论】:

    • 请发布您当前的布局文件。我需要知道你是如何嵌入按钮的。
    【解决方案2】:

    我通过更改解决了问题

    android:layout_width="wrap_content"
    

    android:layout_width="match_parent"
    

    在内部的RelativeLayout中。

    这不是关于按钮,而是关于布局。 :-/

    【讨论】:

      【解决方案3】:

      试试这个.....

      <RelativeLayout 
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent" >
      
          <ImageButton
           android:id="@+id/button_1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_centerInParent="true"
           android:src="@drawable/image1" />          //--your image
      
          <ImageButton
           android:id="@+id/button_2"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignBottom="@+id/button_1"
           android:layout_toEndOf="@+id/button_1"
           android:layout_toRightOf="@+id/button_1"
           android:src="@drawable/image1" />         //--your image
      
          <ImageButton
           android:id="@+id/button_3"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_above="@+id/button_2"
           android:layout_toEndOf="@+id/button_1"
           android:layout_toRightOf="@+id/button_1"
           android:src="@drawable/image1" />         //--your image
      
          <ImageButton
           android:id="@+id/button_4"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_above="@+id/button_1"
           android:layout_alignBottom="@+id/button_3"
           android:layout_toLeftOf="@+id/button_3"
           android:layout_toStartOf="@+id/button_3"
           android:src="@drawable/image1" />        //--your image
      
      </RelativeLayout>
      

      输出...

      【讨论】:

        【解决方案4】:

        布局xml代码:

        <ImageButton
            android:id="@+id/button_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/button_1"
            android:layout_toRightOf="@+id/button_1" />
        
        <ImageButton
            android:id="@+id/button_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@id/button_2"
            android:layout_above="@+id/button_2" />
        
        <ImageButton
            android:id="@+id/button_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@id/button_1"
            android:layout_alignLeft="@id/button_1"/>
        

        这是预览:

        【讨论】:

          猜你喜欢
          • 2014-01-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-17
          • 1970-01-01
          • 2021-12-20
          • 2017-11-05
          相关资源
          最近更新 更多