java - 如何在 LinearLayout 中分隔项目?

标签 java android eclipse android-layout android-linearlayout

我想创建一个如下所示的工具栏:

enter image description here

我如何在 XML 中执行此操作?这是我目前的样子:

<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/toolbarLinearLayout" android:background="@color/solid_yellow">
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/replyButton" android:text="Reply"></Button>
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RT" android:id="@+id/rtButton"></Button>
        <Button android:id="@+id/dmButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="DM"></Button>
    </LinearLayout>

最佳答案

看起来您可能需要 LinearLayout 的填充属性,例如

android:padding="5dip"

要让每个项目占用相同数量的空间,请使用 layout_weight,例如

 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/toolbarLinearLayout" android:background="@color/solid_yellow">
    <Button android:layout_width="fill_parent" android:layout_weight="1" android:layout_height="wrap_content" android:id="@+id/replyButton" android:text="Reply"></Button>
    <Button android:layout_width="fill_parent" android:layout_weight="1" android:layout_height="wrap_content" android:text="RT" android:id="@+id/rtButton"></Button>
    <Button android:id="@+id/dmButton" android:layout_width="fill_parent" android:layout_weight="1" android:layout_height="wrap_content" android:text="DM"></Button>
</LinearLayout>

只要所有元素具有相同的权重,它们就应该占用相同的空间。 layout_width="fill_parent"将确保整个栏都被填满。

关于java - 如何在 LinearLayout 中分隔项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6103960/

相关文章:

java - JVM如何存储类的元信息?

android - 下载的图像可以显示,但托管服务器保存的图像无法在 Android 中显示

java - 在 IntelliJ 中调试(重新加载更改的类)

java - -ear//.war//.POST_MODULE错误(java.lang.NoClassDefFoundError)

java - 如何在执行eclipse Project clean时启动ant clean target

java - 即使加载了所需的 javax.jms 类,ant build (javac) 也会给出 "cannot find symbol"错误

java - 直接调用方法 vs 方法重载

java - 使用嵌入式容器和 JNDI 运行 Spring Boot 应用程序

android - 如何从 ListView onClickAdapter 的数据库中删除一行?

android - 使用参数启动 android 应用程序 - 快捷方式