【问题标题】:Paste Text Field and Button over an ImageView in LinearLayout在 LinearLayout 中的 ImageView 上粘贴文本字段和按钮
【发布时间】:2017-01-17 11:57:39
【问题描述】:

这是我想在我的应用程序中实现的登录活动 (Image2)。但我不知道该怎么做,因为我无法将 editTextpasswordbutton 粘贴到图像的白色部分(整个图像有黑色边框)。该应用程序具有白色背景,我想将图像的外观设为曲线并将其他字段置于其下。现在它看起来像 image1。

看看这个

我还希望我的图像与底部对齐,其宽度为 match_parent 并调整高度,以便将原始图像的比例保存在不同的设备上。这是我的 XML 代码:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:id="@+id/banner"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="5"
    tools:context="com.example.android.start.MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:srcCompat="@drawable/logo"
        android:id="@+id/logo"
        android:layout_weight="2"
    />
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:id="@+id/back"
        app:srcCompat="@drawable/image2"

    />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/editText3" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text="Name"
        android:ems="10"
        android:id="@+id/editText2" />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:layout_centerInParent="true"/>


</LinearLayout>

【问题讨论】:

  • 我认为你应该使用 FrameLayout
  • 使用RelativeLayout下的widgets,把你的图片设置为背景,你的问题就解决了。
  • 使用 FrameLayout,并提供第二个图像作为第二个 FrameLayout 背景。然后从顶部添加具有特定填充的编辑文本
  • 谢谢@Navas pk,使用 FrameLayout 并将图像设置为背景对我有用。

标签: android android-layout android-imageview


【解决方案1】:

你可以这样做

<LinearLayout
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:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="5"
tools:context="com.example.android.start.MainActivity">

<ImageView
    android:id="@+id/logo"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"
    app:srcCompat="@drawable/logo"
    />

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="3"
    android:gravity="center"
    app:srcCompat="@drawable/image2">


    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:inputType="textPassword"/>

    <EditText
        android:id="@+id/editText2"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Button"/>

</LinearLayout>

【讨论】:

  • 谢谢,但它没有用。我设法使用 FrameLayout 解决了我的问题。
【解决方案2】:

使用RelativeLayout 而不是LinearLayout 它有望解决您的问题。

【讨论】:

    【解决方案3】:

    这是我用来实现 Image2 外观的代码:

    <LinearLayout 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:id="@+id/banner"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="5"
        tools:context="com.example.android.start.MainActivity">
    
        <ImageView
            android:id="@+id/logo"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            app:srcCompat="@drawable/logo" />
    
        <FrameLayout
            android:id="@+id/frame"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="3"
            android:background="@drawable/back"
            android:weightSum="2">
             <RelativeLayout
                 android:layout_width="match_parent"
                 android:layout_height="match_parent">
    
                 <EditText
                     android:id="@+id/name"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_gravity="center"
                     android:background="#C8E6C9"
                     android:ems="10"
                     android:hint="Username"
                     android:inputType="textPersonName"
                     android:layout_marginBottom="117dp"
                     android:layout_alignParentBottom="true"
                     android:layout_centerHorizontal="true" />
    
                 <Button
                    android:id="@+id/button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:text="Sign In"
                     android:background="#8BC34A"
                     android:layout_alignParentBottom="true"
                     android:layout_centerHorizontal="true"
                     android:layout_marginBottom="11dp" />
    
                 <EditText
                     android:id="@+id/password"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_gravity="bottom"
                     android:background="#C8E6C9"
                     android:ems="10"
                     android:hint="Password"
                     android:inputType="textPassword"
                     android:layout_above="@+id/button"
                     android:layout_alignLeft="@+id/name"
                     android:layout_alignStart="@+id/name"
                     android:layout_marginBottom="19dp" />
                 />
    
             </RelativeLayout>
    
        </FrameLayout>
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-23
      • 2021-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多