我需要在我的所有活动/视图中包含标题图形.带有头的文件称为头文件.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content"
  android:background="#0000FF" 
  android:padding="0dip">

  <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/header"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="0dip"
    android:layout_marginTop="0dip"
    android:layout_marginBottom="0dip"
    android:padding="0dip"
    android:paddingTop="0dip"
    android:paddingBottom="0dip"
    android:layout_gravity="fill"
    android:background="#00FF00"
    />
</FrameLayout>

注意android:background="#00FF00"(绿色),这只是可视化的目的.

我把它们包含在我的观点中,如下所示:

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

  <include layout="@layout/header" />
  (...)

所以,当我实际try 时,结果看起来像左边的图像,而不是它应该是什么样子(右):

请注意绿色边框

(1)这-橙色部分是有问题的image/ImageView
(2)不受欢迎的绿色边框.注意:通常情况下,绿色区域是透明的-因为我设置了background,所以它才是绿色的.

请注意顶部图像周围的绿色边框;它是ImageView的一部分,我只是想不明白它为什么会在那里,或者我如何才能go 掉它.它将所有填充和边距设置为0(但是当我省略它们时,结果是相同的).图像是480x64px的jpeg*,我把它放在res/Drawable中(虽然不是drawable-Xdpi个中的一个).

(*jpeg,因为我似乎偶然发现了旧的PNG Gamma问题-一开始我解决了这个问题,将绿色边框设置为与图片相同的橙色, colored颜色 不匹配.)

我在我的htc desire/2.2/Build 2.33.163.1和模拟器上试用了它.我还向#android dev;她可以重现这个问题,但也没有解释.构建目标是1.6.

update@tehgoose:这段代码产生完全相同的顶部+底部填充结果.

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

  <!-- <include layout="@layout/header" />  -->

  <ImageView
    android:src="@drawable/header"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#00FF00"
    android:layout_weight="0"
    />

  <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="8dip"
    android:layout_weight="1">

    (... rest of the elements)

  </LinearLayout>
</LinearLayout>

推荐答案

最后

<ImageView
  (...)
  android:adjustViewBounds="true" />

adjustViewbounds attribute人做到了这一点:

如果希望ImageView调整其边界以保持其可绘制视图的纵横比,请将其设置为true.

我跌跌撞撞地跌了upon it here分.谢谢你的帮忙!

Android相关问答推荐

如何使用DeliveromManager.placeCall()删除已拨打的呼叫?

如何在Android Room中使用@Relation多对一查询

泛型类型lambda函数参数作为函数参数

用于小部件泄漏警告的伙伴对象中的Kotlin Lateinit

Android在NavHost中的LazyColumn中编写约束布局:error - replace()在未放置的项目上调用

如何在Android中编写挂起函数和stateflow的单元测试

如何在初始合成期间在可组合函数中调用/获取远程API中的数据[防止无限重组]

Jetpack Compose:带芯片的Textfield

(已解决)从最近的应用程序打开应用程序时出错

Android studio应用判断无法打开离线数据库

如何迭代 SqlDelight Select 结果而不将所有内容加载到内存中?

FFmpeg Android 错误

如何在 Jetpack Compose 中创建无限pager

如何在 React Native 中调试网络响应

组成不重叠的元素

try 使用 ViewPager2 实现滑动视图时出现类型不匹配错误

Jetpack Compose Alignment - 误解了 alignBy 修饰符

使用 Jetpack Compose 时,如何以简单的方式在 Color.kt 中定义 colored颜色 ?

为什么我在 Jetpack Compose 中被警告可选修饰符参数应该具有默认值修饰符?

为什么使用 React Native 和 expo 创建的 APK 体积这么大?