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

时间:2016-09-12 15:35:56

标签: 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_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个项目,我没有将其余的项目保留在确切的中心,我会随机隐藏它们,我需要将剩余的项目均匀分布在相同的空间内。

1 个答案:

答案 0 :(得分:1)

您需要为父布局指定weightSum,然后为每个ImageButton设置layout_weight,如下所示

<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" />

请在此设置LinearLayout的方向,我将其设置为水平,因此所有ImageButton的宽度均为0dp。同样,如果将其设置为垂直,则需要将高度设置为0dp。