Android 我有一个带有四个图像按钮的线性布局,我希望它们均匀地放置在布局中

Android 我有一个带有四个图像按钮的线性布局,我希望它们均匀地放置在布局中,android,android-layout,Android,Android Layout,这是我的布局 <LinearLayout android:id="@+id/buttonLayout" android:layout_width="match_parent" android:layout_height="@dimen/button_layout_height" android:layout_below="@+id/contact_name" android:layout_centerHoriz

这是我的布局

<LinearLayout
        android:id="@+id/buttonLayout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/button_layout_height"
        android:layout_below="@+id/contact_name"
        android:layout_centerHorizontal="true"
        android:layout_marginEnd="@dimen/button_layout_margin_right"
        android:layout_marginStart="@dimen/button_layout_margin_left"
        android:layout_marginTop="@dimen/imagebutton_margin_top"
        android:gravity="center">

        <ImageButton
            android:id="@+id/message_button"
            style="@style/contactCardButtonStyle"
            android:contentDescription="@string/message_button"
            android:src="@mipmap/messageicon"
            android:visibility="gone" />

        <ImageButton
            android:id="@+id/video_call_button"
            style="@style/contactCardButtonStyle"
            android:layout_marginStart="@dimen/imagebutton_margin_left"
            android:contentDescription="@string/videocall_button"
            android:src="@mipmap/videoicon"
            android:visibility="gone"/>

        <ImageButton
            android:id="@+id/audio_call_button"
            style="@style/contactCardButtonStyle"
            android:layout_marginStart="@dimen/imagebutton_margin_left"
            android:contentDescription="@string/audiocall_button"
            android:src="@mipmap/audiocall" />

        <ImageButton
            android:id="@+id/drop_in_button"
            style="@style/contactCardButtonStyle"
            android:layout_marginStart="@dimen/imagebutton_margin_left"
            android:contentDescription="@string/dropin_button"
            android:src="@mipmap/dropinicon"
            android:visibility="gone" />

  </LinearLayout>


如果我隐藏了3个项目,我没有将剩余的一个项目放在准确的中心,我会随机隐藏它们,我需要剩余的项目以相等的空间均匀分布。

您需要为父布局分配一个权重和,然后为每个ImageButton设置布局权重,如下所示

<LinearLayout
    android:id="@+id/buttonLayout"
    android:layout_width="match_parent"
    android:layout_height="@dimen/button_layout_height"
    android:layout_below="@+id/contact_name"
    android:layout_centerHorizontal="true"
    android:layout_marginEnd="@dimen/button_layout_margin_right"
    android:layout_marginStart="@dimen/button_layout_margin_left"
    android:layout_marginTop="@dimen/imagebutton_margin_top"
    android:orientation="horizantal"
    android:gravity="center">

    <ImageButton
        android:id="@+id/message_button"
        android:layout_width="0dp"
        android:layout_height="@dimen/button_layout_height"
        style="@style/contactCardButtonStyle"
        android:contentDescription="@string/message_button"
        android:src="@mipmap/messageicon"
        android:visibility="gone"
        android:layout_weight="1" />

    <ImageButton
        android:id="@+id/video_call_button"
        android:layout_width="0dp"
        android:layout_height="@dimen/button_layout_height"
        style="@style/contactCardButtonStyle"
        android:layout_marginStart="@dimen/imagebutton_margin_left"
        android:contentDescription="@string/videocall_button"
        android:src="@mipmap/videoicon"
        android:visibility="gone"
        android:layout_weight ="1" />

    <ImageButton
        android:id="@+id/audio_call_button"
        android:layout_width="0dp"
        android:layout_height="@dimen/button_layout_height"
        style="@style/contactCardButtonStyle"
        android:layout_marginStart="@dimen/imagebutton_margin_left"
        android:contentDescription="@string/audiocall_button"
        android:src="@mipmap/audiocall" 
        android:layout_weight ="1" />

    <ImageButton
        android:id="@+id/drop_in_button"
        android:layout_width="0dp"
        android:layout_height="@dimen/button_layout_height"
        style="@style/contactCardButtonStyle"
        android:layout_marginStart="@dimen/imagebutton_margin_left"
        android:contentDescription="@string/dropin_button"
        android:src="@mipmap/dropinicon"
        android:visibility="gone" 
        android:layout_weight ="1" />


请在此处设置线性布局的方向。我已将其设置为水平,因此所有图像按钮的宽度均为0dp。同样,如果将其设置为垂直,则需要将高度设置为0dp

对每个图像按钮使用安卓:layout_weight=“1”如何隐藏项目,
消失了
不可见
?并发布
contactCardButtonStyle
请共享当前布局和预期布局的屏幕截图。
。。。间隔均匀的
听起来像是:“使用权重”。感谢我错过的编辑答案的评论。