【问题标题】:Android Space evenly horizontallyAndroid Space 水平均匀分布
【发布时间】:2014-05-29 21:39:15
【问题描述】:

我正在尝试在 Android 中创建“工具栏控件”。
我想添加 4 个水平均匀分布的按钮。
我想到的第一件事是创建一个水平线性布局并添加宽度为 0 且重量相等的按钮。然后按钮将在每个 1/4 宽度的切片中居中。这不是我想要的。
我希望第一个和最后一个按钮贴在边距上,其余按钮在剩余空间中同样对齐。
就像这张图片:

.
按钮是蓝色方块。容器是红色矩形。它应该填充其父级。

【问题讨论】:

  • 子类 LinearLayout 并覆盖 onLayout(),在那里您可以测量孩子并根据需要放置它们,设置边距等...
  • 您可以使用布局:重量。使父级的布局权重为 4,然后为每个蓝色方块设置权重为 1,并将其与边距和填充相结合

标签: android


【解决方案1】:

在你的布局文件中试试这个;

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:weightSum="1" >

    <Button
        android:id="@+id/button1"
        android:layout_width="10dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_weight="0.175"
        android:text="Button" />

    <View
        android:id="@+id/view1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="0.1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="10dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.175"
        android:text="Button" />

    <View
        android:id="@+id/view2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="0.1" />

    <Button
        android:id="@+id/button3"
        android:layout_width="10dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.175"
        android:text="Button" />

    <View
        android:id="@+id/view3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="0.1" />

    <Button
        android:id="@+id/button4"
        android:layout_width="10dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.175"
        android:text="Button" />
</LinearLayout>

【讨论】:

  • 使用weightSum 是一项很棒的技术。但是,对于 OP,如果您仅针对 ICS+ 设备,您可能希望使用轻量级的 Space 小部件,而不是 View
  • 按钮的宽度为 0.175*parent_width。我想要10dp。这可能吗?
  • 请尝试为每个按钮设置 android:maxWidth="10dp"。同时设置 android:layout_weight="1" 和 android:weightSum="7"。
【解决方案2】:

这对我有用。这与 Sameer 的回答类似,但我删除了按钮上的重量,因为我发现它没用:

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

    <Button
        android:id="@+id/button1"
        android:layout_width="10dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Button" />

    <View
        android:id="@+id/view1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="10dp"
        android:layout_height="wrap_content"
        android:text="Button" />

    <View
        android:id="@+id/view2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <Button
        android:id="@+id/button3"
        android:layout_width="10dp"
        android:layout_height="wrap_content"
        android:text="Button" />

    <View
        android:id="@+id/view3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <Button
        android:id="@+id/button4"
        android:layout_width="10dp"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

【讨论】:

    猜你喜欢
    • 2011-05-05
    • 2021-04-23
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    • 2018-03-22
    • 1970-01-01
    • 2013-07-19
    • 1970-01-01
    相关资源
    最近更新 更多