Dav*_*sry 2 android android-edittext
我正试图在我的应用程序中创建这样的东西(取自谷歌文档):
现在,我尝试创建一个TextInputLayout元素并尝试在其周围放置边框,但我无法让它看起来像我发布的图像.
这是我的代码TextInputLayout:
<android.support.design.widget.TextInputLayout
android:id="@+id/shipper_layout"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/text_layout_stroke_normal"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp">
<EditText
android:id="@+id/shipper_field"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp"
android:background="@null"
android:gravity="top"
android:hint="@string/shipper_field"
android:inputType="textMultiLine"
android:overScrollMode="always"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical" />
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
以下是它的外观:(关注之前)
您可以看到边框未按预期更改,并且提示只是最小化
试试这段代码:
在Xml中:
<android.support.design.widget.TextInputLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/shipper_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="@+id/shipper_field"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="@drawable/text_layout_stroke_normal"
android:gravity="top"
android:hint="HELLO"
android:inputType="textMultiLine"
android:overScrollMode="always"
android:padding="15dp"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical" />
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
现在在代码中以programetically方式更改边框和Edittext使用的提示颜色setOnFocusChangeListener
edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
GradientDrawable drawable = (GradientDrawable)edittext.getBackground();
drawable.setStroke(2, Color.RED);
edittext.setHintTextColor(Color.RED);
}
}
});
Run Code Online (Sandbox Code Playgroud)
尝试这个:
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:hint="textArea">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:inputType="textMultiLine"
android:gravity="top"
android:lines="4"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
确保您使用的是最新的材料库:
implementation 'com.google.android.material:material:1.2.1'
Run Code Online (Sandbox Code Playgroud)
尝试这个
在drawable文件夹中创建一个名为custom_borders.xml的xml文件并粘贴以下代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:color="#ff0000" android:width="2dp"></stroke>
<corners android:radius="5dp"></corners>
</shape>
Run Code Online (Sandbox Code Playgroud)
并将其应用到你的TextInputLayout像这样
android:background="@drawable/custom_borders"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4259 次 |
| 最近记录: |