【问题标题】:How to remove margins around ButtonBar without removing them from the rest of an Activity?如何删除 ButtonBar 周围的边距而不将它们从 Activity 的其余部分中删除?
【发布时间】:2013-03-09 22:06:10
【问题描述】:

假设我的布局看起来像这样:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <TextView
        android:id="@+id/greeting"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello, world!" />

    <LinearLayout
        android:id="@+id/actions"
        style="?android:attr/buttonBarStyle"
        android:background="#dddddd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <Button
            android:id="@+id/button1"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="Button1" />

        <Button
            android:id="@+id/button2"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="Button2" />

    </LinearLayout>
</RelativeLayout>

在 Eclipse 中渲染,我得到以下信息:

请注意,ButtonBar 周围有一个边距。有什么办法可以在不影响 Activity 的其余部分的情况下删除它?如果我修改android:padding* 属性,那么“Hello, world!”会碰到 Activity 的左侧。

【问题讨论】:

    标签: android android-activity android-button


    【解决方案1】:

    android:padding* 移动到greeting 元素,将其从父布局中移除。然后该项目单独具有填充,而按钮栏则没有。

    如果您稍后需要添加其他项目(而不仅仅是单个 TextView),您可能希望将它们包装在新布局中并将填充放在该布局上。

    【讨论】:

    • greetingTextView
    • @GeorgeEdison - TextViews 可以像任何其他元素一样具有填充。
    • 哦,我明白你现在在说什么了。但是,如果我想在 Activity 中添加一些其他视图会发生什么?我必须为它们中的每一个指定填充吗?有没有办法让 Activity 中的所有内容都具有相同的填充?
    • @GeorgeEdison - 我编辑了我的答案 - 最简单的方法是将所有这些视图包装在一个新布局中(例如,RelativeLayout 或 LinearLayout)并将该布局仅用于填充目的。
    • 阅读您的编辑后,我刚刚完成了该操作。现在一切正常 - 谢谢!
    【解决方案2】:

    为您创建的每个布局设置 android:padding... 放置在父布局中的孩子将为他们设置填充...您可以改用 layout_margin...

    【讨论】:

      猜你喜欢
      • 2013-02-07
      • 1970-01-01
      • 2018-02-04
      • 1970-01-01
      • 1970-01-01
      • 2021-03-23
      • 1970-01-01
      • 2012-07-15
      • 1970-01-01
      相关资源
      最近更新 更多