【问题标题】:Is there a way to completely eliminate padding in a LinearLayout containing buttons?有没有办法完全消除包含按钮的 LinearLayout 中的填充?
【发布时间】:2011-05-06 01:15:48
【问题描述】:

我尝试了以下方法以尝试在水平线性布局中显示两个按钮,按钮的边缘与线性布局的边框齐平。令我惊讶的是,我发现按钮与线性布局的左、右和下边缘之间总是有填充。只有按钮的顶部边缘最终与线性布局的边框齐平。这是为什么?有没有办法控制这种行为?非常感谢。

<LinearLayout android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="#777"
  android:padding="0dip"
  android:layout_margin="0dip">
  <Button android:id="@+id/feeling_done_button"
     android:text="@string/done_button_label"
     android:layout_height="wrap_content"
     android:layout_width="match_parent"
     android:layout_weight="1"
     android:padding="0dip"
     android:layout_margin="0dip"/>
  <Button android:id="@+id/feeling_cancel_button"
     android:text="@string/cancel_button_label"
     android:layout_height="wrap_content"
     android:layout_width="match_parent"
     android:layout_weight="1"
     android:padding="0dip"
     android:layout_margin="0dip"/></LinearLayout>

【问题讨论】:

    标签: android android-layout android-linearlayout padding


    【解决方案1】:

    如果您的意思是希望它们被逐个像素地推到边缘,您需要使用自己的 9 补丁作为按钮背景,因为 Android 中的默认补丁周围有一些额外的像素其 9-patch 背景的边界。有关这方面的示例,请查看 SDK 文件夹中的文件:

    platforms/android-8/data/res/drawable-hdpi/btn_default_transparent_normal.9.png

    例如,这是我在默认情况下得到的:

    还有我之前制作的自定义 9 补丁:

    【讨论】:

    • 嗨,出于某种原因,使用 9 补丁作为按钮仍然使用大量填充,而使用 textView 则不会。怎么可能?
    猜你喜欢
    • 2017-06-13
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2011-07-04
    • 1970-01-01
    • 2013-02-17
    • 1970-01-01
    相关资源
    最近更新 更多