【问题标题】:Can't set the background of my EditText无法设置我的 EditText 的背景
【发布时间】:2014-06-12 21:49:25
【问题描述】:

我的片段 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.gelasoft.answeringball.MainActivity$PlaceholderFragment" 
    android:background="@drawable/wg_blurred_backgrounds_14"
    >

    <ImageView
        android:id="@+id/sphereIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:contentDescription="@string/magicBallDescr"
        android:src="@drawable/rsz_31mystic" />

    <LinearLayout
        android:id="@+id/table"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/sphereIcon"
        android:orientation="vertical" >

        <TextView 
            android:id="@+id/txtAnswer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />

        <EditText
            android:id="@+id/txtQuestion"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:hint="@string/textHint"
            android:inputType="textMultiLine"
            android:lines="6" />

        <Button 
            android:id="@+id/btnAsk"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:text="@string/btnAskQ"     
            android:paddingEnd="@dimen/activity_vertical_margin"      
         />

    </LinearLayout>
</RelativeLayout>

这是它的外观:

现在我想在EditText 上设置一个不同的背景图片,所以我编辑了 xml 并使其如下:

<EditText
    android:id="@+id/txtQuestion"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:hint="@string/textHint"
    android:inputType="textMultiLine"
    android:lines="6" 
    android:background="@drawable/background"/>

但现在我在EditText 上设置的背景占据了整个LinearLayout 空间。

以下是更改后在编辑器中的外观:

为什么它占据了整个位置并隐藏了按钮?

我在这里缺少什么?我敢肯定它很小,但我无法发现它。

【问题讨论】:

  • 背景图片的尺寸是多少?好像有 id Out of Memory 错误。
  • 在更改后尝试清理您的项目,同时确保您的 editText id 与您的活动代码中的相同,并且您在 setContentView 中使用了正确的 xml
  • @vjdhama 哦。它是 273 508 字节。好的 解决了一个问题,但是为什么它需要整个LinearLayout 而不是EditText

标签: java android xml android-fragments android-activity


【解决方案1】:

我不确定,但我觉得你的背景图片太大了。您可以尝试替换它的小宽度和高度类型的图像。希望你能解决这个问题。祝你好运!

【讨论】:

    【解决方案2】:

    您遇到了内存不足异常,如堆栈跟踪的最后几行所示。

    解决方案是减小用作背景的可绘制对象的大小。它只是一个 EditText,而且它的小尺寸不保证高分辨率文件作为背景。
    这可以使用各种图像编辑软件来完成,从 Photoshop 到 Microsoft Paint。
    带油漆:
    第 1 步:在 Paint 中打开图像。
    第 2 步:点击顶部工具箱旁边的“调整大小”按钮。
    第 3 步:将图像大小减小到适当的值。
    第 4 步:将图像保存在可绘制文件夹中。
    第 5 步:如果您使用的是 Eclipse,请右键单击您的项目并单击“刷新”
    第 6 步:再次启动您的应用。

    现在为 EditText 的扩展:

    您将 EditText 的高度设置为 wrap_content。当您将图像设置为背景时,它也将成为它的内容。因此,您的 EditText 会膨胀以适应它的新背景。

    一个解决方案是对高度进行硬编码,例如
    android: layout_height="30dp" 虽然能否使用它取决于您的要求。

    【讨论】:

    • 好吧,我刚刚修好了,但是为什么EditText的背景图片占据了整个lLinearLayout,连按钮都隐藏了?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-19
    • 1970-01-01
    • 1970-01-01
    • 2012-02-12
    • 1970-01-01
    相关资源
    最近更新 更多