【问题标题】:Border in layout has a unexpected space布局中的边框有一个意想不到的空间
【发布时间】:2013-12-25 07:57:03
【问题描述】:

我有一个带边框的布局。我的问题是,边框和其他布局之间有一个空格,但我不知道它来自哪里。

我的布局看起来像

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/BackgroundLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
<!-- Layout come's here :) -->
</LinearLayout>

BackgroundLayout 的样式如下:

<style name="BackgroundLayout">
    <item name="android:background">@drawable/frame_border</item>   
    <item name="android:orientation">vertical</item>
    <item name="android:paddingLeft">@dimen/line_width</item>
    <item name="android:paddingRight">@dimen/line_width</item>
</style>

frame_border(是一个drawable.xml)看起来像:

<?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
      <item>
        <shape android:shape="rectangle" >
          <stroke
              android:width="@dimen/line_width"
              android:color="@color/background_color" />
          <solid android:color="@color/background_color" />

        </shape>
      </item>
      <item
          android:left="@dimen/line_width"
          android:right="@dimen/line_width"
          android:bottom="@dimen/line_width">
        <bitmap android:src="@drawable/background" android:gravity="fill_horizontal|fill_vertical"     />
      </item>
</layer-list>

@dimen/line_width 的大小为 5dp。

我的样式中的填充与边框的大小相同。所以应该有任何差异(希望如此)。

编辑: 为了演示这个问题,我创建了一个新视图(见下文)。背景保持不变。视图(LinearLayout为黑色区域):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frame_container"
    style="@style/BackgroundLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/black"
        android:orientation="vertical"
        android:weightSum="1" >
    </LinearLayout>
</FrameLayout>

解决方案

到目前为止,我的解决方案是将背景图像位置从@dimen/line_width (5dp) 更改为 5.5dp。不令人满意,但它有效。

  <item
      android:left="5.5dp"
      android:right="5.5dp"
      android:bottom="5.5dp">
    <bitmap android:src="@drawable/background" android:gravity="fill_vertical|fill_horizontal"/>
  </item>

【问题讨论】:

    标签: android android-layout android-drawable


    【解决方案1】:

    来自:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        style="@style/BackgroundLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
       <!-- Layout come's here :) -->
     </LinearLayout>
    

    您的样式中有内边距。检查并发布结果。

    【讨论】:

    • 内边距与蓝色边框的大小相同。所以不应该有任何空间。
    【解决方案2】:

    编辑:不是 100% 确定,但可能是库存 android 分隔器的问题。以下代码适用于我。

    <LinearLayout 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:background="@drawable/border"
        android:padding="4dip"
        tools:context=".MainActivity" >
    
        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:divider="@drawable/horizontal_divider"
            android:dividerHeight="4dp" >
        </ListView>
    
    </LinearLayout>
    

    背景.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >
        <solid android:color="#fd9f8d" />
        <stroke android:width="4dip" android:color="#BDBCBC"/>
    </shape>
    

    horizo​​ntal_divider.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="line">
        <stroke
            android:color="#FFFFFF"
            android:width="2dip"
        />
        <size
            android:height="1dip"
        />
    </shape>
    

    【讨论】:

    • 内边距应与边框大小相同(参见drawable.xml)
    • 如果您希望子元素(我假设是您的列表视图)完全“match_parent”,您仍然不需要在父元素上进行填充
    • 可能与股票 android 分隔符有关?见编辑。
    • 我使用了一个自定义的安卓分隔线。我也遇到了其他观点的问题。我编辑了我的问题并添加了另一个示例。
    • 抱歉写了这么多 cmets。我有一个解决方案,请参阅我的编辑。它不令人满意,但它有效。
    猜你喜欢
    • 2017-01-07
    • 1970-01-01
    • 2022-01-01
    • 2012-09-02
    • 2013-02-24
    • 1970-01-01
    • 2020-09-11
    • 1970-01-01
    • 2013-11-28
    相关资源
    最近更新 更多