【问题标题】:Custom Background for editText in androidandroid中editText的自定义背景
【发布时间】:2018-02-28 06:29:02
【问题描述】:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
    <solid android:color="#FFFFFF"/>
    <stroke
    android:width="1.5dp"
    android:color="#E53935"
        />

        </shape>

我想为 editText 进行这种类型的自定义设计。

【问题讨论】:

  • 哪里出错了?
  • Custom designing EditText的可能重复
  • 没有错误。我只想对上面给出的照片进行编辑文本。我当前的代码没有给我实际的 editText 设计。

标签: android android-layout material-design


【解决方案1】:

试试这个:

您的自定义文件是完美的,只是您需要在后台使用它 编辑文本

<LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#ffffff"
        xmlns:android="http://schemas.android.com/apk/res/android">


        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="User name"
            android:padding="15dp"
            android:layout_margin="10dp"
            android:background="@drawable/et_cust"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Password"
            android:padding="15dp"
            android:layout_marginTop="20dp"
            android:layout_margin="10dp"
            android:background="@drawable/et_cust"/>


    </LinearLayout>

et_cust.xml

 <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="#FFFFFF"/>

    <stroke
        android:width="2dp"
        android:color="#c9837f"/>

</shape>

【讨论】:

【解决方案2】:

将其用于 EditTextandroid:background="@drawable/background_et"

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#FFFFFF" /> 

    <stroke android:color="#FF0000"
        android:width="2dp"></stroke>

    <corners android:radius="0dp" />
</shape>

还有这个按钮:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#FF0000" /> 

    <stroke android:color="#FFFFFF"
        android:width="0dp"></stroke>

    <corners android:radius="0dp" />
</shape>

【讨论】:

    【解决方案3】:

    将 Drawable 文件 custom_style.xml 添加到您的 Drawable 文件夹中 -

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid android:color="@color/white" />
        <stroke
            android:width="1dp"
            android:color="#000000" />
    
    </shape>
    

    然后将其用作 EditText 的背景。

    <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Your EditText"
            android:padding="10dp"
            android:background="@drawable/edit_text_bg"/>
    

    【讨论】:

      【解决方案4】:

      在你的xml文件中插入你的drawable作为背景:

      android:background="@drawable/your_drawable"
      

      【讨论】:

        【解决方案5】:

        您所要做的就是将描边设置为您的背景。!
        background_stroke.xml

         <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
                <solid android:color="@android:color/white" />
                <stroke android:width="1dp" android:color="#E53935"/>
            </shape>
        

        将此添加到 xml:

         <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:background="@drawable/background_stroke"/>
        

        【讨论】:

          【解决方案6】:

          当你为 edittext 创建这个 xml 时

          <?xml version="1.0" encoding="utf-8"?>
          <shape xmlns:android="http://schemas.android.com/apk/res/android"
              android:shape="rectangle">
              <solid android:color="@color/white" />
              <stroke
                  android:width="1dp"
                  android:color="#ED1C22" />
          
          </shape>
          

          现在你需要像这样使用这个xml到EditText背景中。

          android:background="@drawable/edit_text_bg"
          

          你最终的活动 xml 将是

          <?xml version="1.0" encoding="utf-8"?>
          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:padding="16dp">
          
              <EditText
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:background="@drawable/edit_text_bg"
                  android:hint="User name"
                  android:paddingBottom="8dp"
                  android:paddingLeft="16dp"
                  android:paddingRight="16dp"
                  android:paddingTop="8dp"
                  android:textColor="@color/black"
                  android:textSize="14dp" />
          
              <EditText
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginTop="8dp"
                  android:background="@drawable/edit_text_bg"
                  android:hint="Password"
                  android:paddingBottom="8dp"
                  android:paddingLeft="16dp"
                  android:paddingRight="16dp"
                  android:paddingTop="8dp"
                  android:textColor="@color/black"
                  android:textSize="14dp" />
              <Button
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:background="#ED1C22"
                  android:layout_marginTop="8dp"
                  android:text="LOGIN"
                  android:textColor="@color/white"/>
          </LinearLayout>
          

          【讨论】:

            【解决方案7】:
            <?xml version="1.0" encoding="UTF-8"?>
            <shape xmlns:android="http://schemas.android.com/apk/res/android">
                <stroke android:width="0.2dp" android:color="#FF0000" />
                <padding android:left="2dp"
                    android:top="2dp"
                    android:right="2dp"
                    android:bottom="2dp" />
            </shape>
            

            将背景设置为edittext。

            【讨论】:

              猜你喜欢
              • 2017-03-23
              • 2012-02-12
              • 2020-09-03
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-04-10
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多