我想通过布局标签中的变量标签将我的编辑文本大小更改为数据绑定(bind),但我无法设置合适的大小。
我在这里添加我的自定义编辑文本布局,我也粘贴我的布局,所以请指导我在布局中使用此编辑文本时如何设置动态文本大小。
这是自定义编辑文本布局,我在其中声明了 textSize 变量,类型为 Float
<layout 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">
<data>
<import type="android.text.InputType" />
<variable name="textSize" type="Float" />
<variable name="inputType1" type="Integer" />
<variable name="visibility" type="Boolean" />
<variable name="textColor" type="Integer" />
<variable name="holderColor" type="Integer" />
<variable name="hintColor" type="Integer" />
<variable name="holderText" type="String" />
<variable name="hintText" type="String" />
<variable name="holderVisibility" type="Boolean" />
<variable name="leftIcon" type="android.graphics.drawable.Drawable" />
<variable name="editTextBottomLine" type="android.graphics.drawable.Drawable" />
<variable name="rightIcon" type="android.graphics.drawable.Drawable" />
<variable name="fontStyle" type="Integer" />
<variable name="textField" type="androidx.databinding.ObservableField<String>" />
<variable name="isTypePhoneNo" type="Boolean" />
<variable name="contentMaxLength" type="Integer" />
<variable name="emojiVisibility" type="Boolean" />
<variable name="clickListener" type="android.view.View.OnClickListener" />
<variable name="hasFocus" type="androidx.databinding.ObservableBoolean" />
<variable name="enableState" type="Boolean" />
<variable name="textWatcher" type="android.text.TextWatcher" />
<variable name="onFocus" type="android.view.View.OnFocusChangeListener" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/cl_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:drawableStart="@{leftIcon}"
android:drawableEnd="@{rightIcon}"
android:drawablePadding="5sp"
android:fontFamily="@font/inter_regular"
android:hint="@{hintText}"
android:inputType="@{inputType1}"
android:maxLength="@{contentMaxLength?? 256}"
android:onClick="@{clickListener}"
android:text="@={textField}"
android:textColor="@{textColor}"
android:textColorHint="@{hintColor?? @color/white}"
android:textCursorDrawable="@drawable/cursor_color"
android:textSize="@{textSize ?? @dimen/edit_text_size_26dp}"
app:hasFocus="@{hasFocus}"
app:isPhoneNoFormat="@{isTypePhoneNo}"
app:keyListener="@{enableState??false}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:textWatcher="@{textWatcher}"
app:onFocus="@{onFocus}"
tools:text="@string/dummy_hint_edittext" />
</androidx.constraintlayout.widget.ConstraintLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="10dp"
android:background="@{editTextBottomLine ?? @drawable/dot_line_gray}"
app:layout_constraintTop_toBottomOf="@+id/cl_edit_text" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:clickable="true"
android:onClick="@{clickListener}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:visibility="@{enableState?? false}" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
这是我的布局类,我在其中使用此布局,在这里我在此布局中声明 textSize,但它不起作用。
<include
android:id="@+id/et_userId"
layout="@layout/edittext_without_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
**app:textSize="@{18}"**
android:layout_marginTop="@dimen/line_spacing_9dp"
app:hintColor="@{@color/place_holder_color_dark}"
app:layout_constraintTop_toBottomOf="@+id/textView2"
app:hintText="@{@string/dummy_user_id}"
app:inputType1="@{InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE}"
app:textColor="@{@color/color_black_welcome}"
app:textField="@={viewModel.userId}"
app:editTextBottomLine="@{@drawable/dot_line_gray}"/>
说明:首先,我使用变量标签 textSize 创建了自定义编辑文本布局,之后,我在另一个屏幕布局中使用此布局,并在该屏幕上尝试通过 textSize 变量设置编辑文本大小。 我也尝试通过使用 Integer 更改 textSize 变量值,但这没有用。因此,如果有人对此有解决方案,那对我来说非常好。
最佳答案
我认为最好的方法是添加一个 BindingAdapter
先添加这部分java代码
注意这个方法必须是静态的
@BindingAdapter("android:textSize")
public static void bindTextSize(TextView textView, int size) {
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
}
现在您可以为 TextView 添加文本大小并使用 int 编辑文本,示例:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@{12}" />
关于android - 数据绑定(bind)中通过变量标签设置的文本大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58987579/