【问题标题】:Adding a background image to RelativeLayout hide all others elements将背景图像添加到 RelativeLayout 隐藏所有其他元素
【发布时间】:2015-05-14 01:01:58
【问题描述】:

我有一个简单的 XML:

<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"
    tools:context=".LoginActivity"
    android:background="@drawable/login_background">


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="320dp"
        android:layout_height="160dp"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="218dp"
        android:layout_marginBottom="25dp">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/editText"
            android:textColor="@color/surtimax_gris_texto"
            android:layout_gravity="center_horizontal"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:textStyle="bold"
            android:hint="@string/login_edittext_user_text"
            android:textColorHint="@color/surtimax_gris_texto" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/editText2"
            android:background="@drawable/login_edittext_background"
            android:textColor="@color/surtimax_gris_texto"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="15dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:textStyle="bold"
            android:hint="@string/login_edittext_pass_text"
            android:textColorHint="@color/surtimax_gris_texto" />
    </LinearLayout>
</RelativeLayout>

问题是:在模拟器或真实设备中都没有显示 LinearLayout 和 EditTextRelativeLayout 有背景时。
如果我只是删除背景一切正常......这很奇怪。

不是关于应用的错误日志。

minSdkVersion 17
targetSdkVersion 22

更新 它使用普通背景图像(非 9patch)。所以问题在于背景 9patch 图像。

更新 2 关于在 SO 中在后台使用 9patch 有很多问题/答案,但是因为我面临的问题是应用背景,所以我开始在谷歌中寻找“背景图像问题”。当然,谷歌的结果都没有给我任何有用的答案。当凯就这个问题给我和建议时,我很容易找到问题所在。所以我不打算结束这个问题,因为它可以帮助某人完成。

【问题讨论】:

  • login_background 是 9patch,如果将背景更改为其他可绘制对象,也会发生同样的事情吗?
  • 是的,login_background 是一个 9patch,但我没有尝试过其他可绘制对象。我明天试试。
  • @peter 普通背景图片不会发生这种情况。你知道我能做些什么来解决它吗?
  • 好的,我刚刚将我的谷歌搜索修复为 9patch 背景图像问题,我得到了一些问题/答案,这些问题/答案给了我和正在发生的事情的建议:我没有在 9patch 图像中定义“内容区域”。链接是:link1link2。因为你是让我找到答案的人,所以欢迎你发布答案,我会接受的。
  • 谢谢,请注意,虽然设置填充有效,但在正常情况下,最好同时提供拉伸定义和可绘制定义

标签: android background android-edittext android-linearlayout android-relativelayout


【解决方案1】:

问题在于您的背景设置为 9patch 图像,该图像未定义拉伸区域(左角和上角)和可绘制区域(右角和下角)。

解决方案是修改 9patch 图像以包含必要的信息,或者将填充设置为 0(XML 中的android:padding="0dip" 或代码中的setPadding(0))。

【讨论】:

    猜你喜欢
    • 2020-05-28
    • 1970-01-01
    • 2013-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-12
    • 2014-11-03
    相关资源
    最近更新 更多