如何正确调整此图像的大小并将editText放入其中?

时间:2016-11-02 15:36:22

标签: android xml imageview

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

enter image description here

图像的大小为297x199。在android上我想把一个黑色的EditText放到这个图像中,如下所示: enter image description here

但是当我尝试将它放在我的项目上时,我得到了这个: enter image description here

并且无法在此处添加和编辑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>

有办法做到这一点吗?这张图片的尺寸是否合适?

1 个答案:

答案 0 :(得分:0)

认为我正确理解你的问题。我想你只想让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>

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