android - 从 RelativeLayout 扩展的自定义复合视图。子视图的定位不正确
问题描述
我想制作包含 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标签是为了优化视图的绘制,避免不必要的嵌套布局
解决方案
首先你不需要有
android:layout_above="@id/editTextHint"
并添加
android:layout_gravity="center_vertical"
编辑TextHint 和 textViewContent
也可能你应该删除
android:layout_marginEnd="4dp"
或改变一点它的价值
推荐阅读
- c++ - 有没有办法在 C++ 中更新对向量中的一对
- reactjs - 我可以从另一个组件更改一个组件状态吗?
- ssis - SSIS 发送邮件任务 - 超链接重命名
- r - 如何从R中的数据框的列中给出数组元素名称
- r - 错误:Quosures 只能在 quasiquotation 上下文中不被引用,功能与 plotly
- rest - 关于改善内存和性能方面的文件上传 - struts 1
- php - Symfony Messenger 在处理程序中获取重试次数
- javascript - Javascript 导入在对 android 发布版本的原生反应中失败
- istio - envoy lua 过滤器 - envoy_on_response() 方法的授权标头
- google-cloud-platform - Terraform 和 GCP 作曲家设置