【问题标题】:How to resize correctly this image and put editText into it?如何正确调整此图像的大小并将editText放入其中?
【发布时间】:2016-11-02 15:36:22
【问题描述】:

我有这个问题。我想把这个布局放在这张图片上:

图片尺寸为 297x199。在 android 上,我想在这个图像中放一个黑色的 EditText,如下所示:

但是当我尝试将它放在我的项目中时,我得到了这个:

并且无法在此处添加正确大小的 EditText。

我项目的xml是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="EBEBF1"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="230dp"
        android:layout_marginTop="9dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="9dp"
        android:layout_marginRight="9dp">

        <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


            <TextView
                android:text="@string/cas"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="39dp"
                android:layout_marginStart="39dp"
                android:id="@+id/textView2"
                android:textStyle="normal|bold"
                android:textSize="18sp"
                android:textColor="#000"
                android:layout_marginBottom="16dp"
                android:layout_alignBottom="@+id/imageView11"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />

            <ImageView
                android:layout_width="250dp"
                app:srcCompat="@drawable/photoframe"
                android:id="@+id/imageView11"
                android:layout_marginTop="49dp"
                android:layout_height="90dp"
                android:layout_alignParentTop="true"
                android:layout_alignLeft="@+id/textView2"
                android:layout_alignStart="@+id/textView2"
                android:layout_marginLeft="22dp"
                android:layout_marginStart="22dp" />

            <EditText
                android:layout_height="35dp"
                android:inputType="numberSigned"
                android:ems="10"
                android:background="#000"
                android:id="@+id/editText2"
                android:layout_width="65dp"
                android:layout_marginLeft="46dp"

                android:layout_alignBottom="@+id/textView2"
                android:layout_toRightOf="@+id/textView2"
                android:layout_marginStart="46dp" />

        </RelativeLayout>

    </android.support.v7.widget.CardView>

</RelativeLayout>

有没有办法做到这一点?此图片的尺寸是否正确?

【问题讨论】:

  • 您不能将任何东西放入 ImageView。你可以撅撅撅 它。但是您可以将图像作为 View 的背景。包括一个 EditText。对于“正确的大小”,我会改用 9 补丁。
  • 唯一的方法是使用 Photoshop 等调整图像大小?
  • 为什么不使用 9 补丁?
  • @Rotwang 9 补丁是如何工作的?

标签: android xml imageview


【解决方案1】:

认为我正确理解了您的问题。我认为您只希望 EditText 推送到背景图像的边框。对于我的示例,我只是在 EditText 周围放置了 10dp 的边距。希望这会有所帮助!

<!--Adjust with dimensions for the dimensions of the container and image view-->
<FrameLayout
    android:layout_width="297dp"
    android:layout_height="199dp">

    <ImageView
        android:id="@+id/background"
        android:layout_width="297dp"
        android:layout_height="199dp"
        android:src="@drawable/photoframe"/>

    <!--Adjust margins as you see fit-->
    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:background="#000"/>

</FrameLayout>

然后,您将根据需要在布局中的某个位置插入框架布局。另外:请记住,像素尺寸会因设备而异,对于更高密度的显示器,如果您没有适合所有像素密度的图像,则会出现一些图像质量下降。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 2011-03-26
    • 1970-01-01
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    相关资源
    最近更新 更多