我想制作包含 EditText 和 TextView 的小部件,看起来像这样:
。
为了实现这一点,我创建了RelativeLayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="48dp">
<EditText
android:id="@+id/editTextHint"
style="@style/CustomEditTextTheme"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="14dp"
android:textColor="@color/textColor"
android:textSize="16sp"
android:hint="To my first character"
tools:text="Тип клиента">
</EditText>
<TextView
android:id="@+id/textViewContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editTextHint"
android:layout_alignParentEnd="true"
android:layout_above="@id/editTextHint"
tools:text="Продавец"
android:fontFamily="@font/proxima_nova_regular"
android:textSize="16sp"
android:layout_marginEnd="4dp"
android:textColor="@color/colorPrimary"/>
</RelativeLayout>
上面的 RelativeLayout 效果很好,看起来像计划的那样。为了制作复合视图,我删除了</RelativeLayout>
标签并将其包装到</merge>
(我不应用代码,因为除了 RelativeLayout 标签之外它是相同的)
为了使用视图,我编写了扩展 RelativeLayout 的 MyCustomEditText 类
public class CustomEditText extends RelativeLayout {
private EditText editTextHint;
private TextView textViewEntry;
private String hintText;
private String inputText;
private String starterText;
private OnCustomEditTextListener listener;
public String getHintText() {
return hintText;
}
public String getInputText() {
return inputText;
}
public void setHintText(String hintText) {
editTextHint.setText(hintText);
}
public void setInputText(String inputText) {
textViewEntry.setText(inputText);
}
public CustomEditText(Context context) {
super(context);
initializeViews(context);
}
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray;
typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomEditText);
hintText = typedArray.getString(R.styleable.CustomEditText_hintText);
inputText = typedArray.getString(R.styleable.CustomEditText_inputText);
starterText = typedArray.getString(R.styleable.CustomEditText_starterText);
typedArray.recycle();
initializeViews(context);
}
private void initializeViews(Context context) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.custom_edit_text, this);
setMinimumHeight(48);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
editTextHint = findViewById(R.id.editTextHint);
textViewEntry = findViewById(R.id.textViewContent);
bindDataToChildViews();
editTextHint.setOnClickListener(v -> listener.onClick());
}
private void bindDataToChildViews() {
editTextHint.setText(hintText);
if ((inputText == null)||(inputText.isEmpty())) {
textViewEntry.setText(starterText);
} else {
textViewEntry.setText(inputText);
}
}
public void setHintClickListener(OnCustomEditTextListener listener) {
this.listener = listener;
}
public interface OnCustomEditTextListener {
void onClick();
}
}
我正在尝试在其他布局中使用此视图,但文本视图的定位很难看:
我认为从 RelativeLayout 扩展视图足以正确定位。所以我需要你的帮助来定义我哪里出错了。如何正确定位元素?
PS 将RelativeLayout替换为merge标签是为了优化视图的绘制,避免不必要的嵌套布局