【问题标题】:How to make an underline below TextView and EditText in Android如何在 Android 中的 TextView 和 EditText 下方添加下划线
【发布时间】:2020-01-02 22:37:40
【问题描述】:

我想实现这样的目标,我可以只使用 TextView 但与它相邻的 EditText,我被卡住了。

这是我目前的代码 -

可绘制 -

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="line">
    <stroke android:color="#3F51B5" android:width="2dp"/>
    <padding android:top="-30dp" />
</shape>

布局 -

                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:background="@drawable/signup_screen_input_field"
                android:layout_marginTop="20dp"
                android:orientation="horizontal">

            <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:padding="5dp"
                    android:text="UserName"
                    android:textColor="@android:color/black"/>

            <EditText
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@android:color/transparent"
                    android:gravity="center"
                    android:hint="John Doe"
                    android:padding="5dp"
                    android:textColor="@android:color/white"
                    android:textColorHint="@android:color/white"/>

        </LinearLayout>

我的代码也很老套,不好。 我正在努力实现的形象 -

【问题讨论】:

标签: android android-layout user-interface textview android-edittext


【解决方案1】:

这个视图应该在你的 TextView 和 EditText 下画一条全宽的线。

<View 
   android:layout_width="fill_parent"
   android:layout_height="1dp"       
   android:background="#00ffff" />

布局 XML 应如下所示

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="5dp"
            android:text="@string/username"
            android:textColor="@android:color/black" />

        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:hint="@string/john_doe"
            android:padding="5dp"
            android:textColor="#ff0000"
            android:textColorHint="#ff0000" />

    </LinearLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#00ff00" />

</LinearLayout>

你可以在下面看到它的样子,当然不要忘记改变颜色。

【讨论】:

  • 不,没有用。视图标签应该在线性布局中吗?
  • 我编辑了我的答案,希望它现在可以按预期工作,您必须添加另一个垂直线性布局...如果您有多个视图组,请考虑使用可以轻松扁平化 XML 结构的约束布局。
猜你喜欢
  • 2011-12-23
  • 1970-01-01
  • 2017-09-06
  • 2017-03-17
  • 1970-01-01
  • 1970-01-01
  • 2017-03-12
  • 2012-05-08
  • 2013-03-31
相关资源
最近更新 更多