【问题标题】:How to get rid of the extra gap between a button and other views?如何摆脱按钮和其他视图之间的额外差距?
【发布时间】:2010-02-17 01:29:45
【问题描述】:

当我创建一个按钮视图时,Android 总是在这个按钮和它下面的其他视图之间创建一些额外的空间。

在下面的示例中,第二个按钮上方有一个按钮。您可以看到这两个按钮之间的差距。我怎样才能摆脱这个差距?谢谢。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <Button android:id="@+id/up"
        android:layout_width="fill_parent" android:layout_height="28dip"
        android:textSize="9sp" android:gravity="center" android:text="Hide sth"
        android:layout_marginBottom="0" android:paddingBottom="0" />
    <Button android:id="@+id/down"
        android:layout_width="fill_parent" android:layout_height="28dip"
        android:textSize="9sp" android:gravity="center" android:text="Show sth" />
   </LinearLayout>

【问题讨论】:

  • 你可以找到你的解决方案here尝试使用android:layout_margin属性享受! ;)

标签: android


【解决方案1】:

创建您自己的按钮背景九补丁PNG,以消除空间。您可以在 SDK 中找到现有的背景文件。

【讨论】:

    【解决方案2】:

    CommonsWare 几乎为您指明了正确的方向,但这里有一个示例布局示例。只需将 android:background="COLOR" 替换为 9 补丁 png 的引用

    
    &lt;?xml version="1.0" encoding="utf-8"?>
    &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    
    
    <Button
        android:id="@+id/btn1"
        android:text="btn1"
        android:padding="0dip"
        android:layout_margin="0dip"
        android:layout_width="fill_parent"
        android:layout_height="40dip"
        android:background="#FFF"
    />
    
    <Button
        android:id="@+id/btn2"
        android:text="btn2"
        android:padding="0dip"
        android:layout_margin="0dip"
        android:layout_width="fill_parent"
        android:layout_height="40dip"
        android:background="#FF0000"
    />
    

    &lt;/LinearLayout&gt;

    希望有帮助!

    【讨论】:

      【解决方案3】:

      我只是想出了最简单的方法。我们必须简单地为Buttons 添加背景,它会自动减少空格。 注意示例

      <Button
          android:id="@+id/button1"
          android:text="Button 1"
          android:layout_width="match_parent"
          android:layout_height="100dp"
          android:background="#000000"  />
      
      <Button
          android:id="@+id/button2"
          android:text="Button 2"
          android:layout_width="match_parent"
          android:layout_height="100dp"
          android:background="#000000" />
      

      显然我们必须用边界线分割Buttons。有几种方法。

      1. android:layout_marginTop="1dp" 到第二个 Button。一小块背景将它分开。

      2.Button 添加style

      <?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="rectangle">
          <gradient android:startColor="#FFFFFF" 
               android:endColor="#000000"
               android:angle="270" />
          <corners android:radius="3dp" />
          <stroke android:width="1px" android:color="#FFFFFF" />
      </shape>
      

      并设置它:

      android:background="@drawable/your_button_border"
      

      3.View添加不同颜色的背景并将其置于Buttons之间。

      <View
          android:layout_width="match_parent"
          android:layout_height="2dp"
          android:background="#AAA" />
      

      只选择最方便的方式。

      【讨论】:

        猜你喜欢
        • 2011-04-21
        • 1970-01-01
        • 2020-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-24
        相关资源
        最近更新 更多