自定义复合视图从RelativeLayout扩展。子视图的位置不正确

时间:2018-11-30 14:18:15

标签: android android-custom-view android-relativelayout android-compound-view

我想制作包含EditText和TextView的小部件,如下所示: Custom Edit text

为此,我创建了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标记之外,其他代码均相同)

要使用视图,我编写了MyCustomEditText类,该类扩展了RelativeLayout

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();
}
}

我正在尝试在其他布局中使用此视图,但是文本视图的位置很难看:

enter image description here

我认为从RelativeLayout扩展视图足以正确定位。因此,我需要您的帮助来确定我要去哪里。如何正确定位元素?

P.S。可以将RelativeLayout替换为merge标签,以优化视图绘制并避免不必要的嵌套布局

1 个答案:

答案 0 :(得分:0)

首先您不需要拥有

android:layout_above="@id/editTextHint"

然后添加

android:layout_gravity="center_vertical"

编辑textHint和textViewContent

也许您应该删除

android:layout_marginEnd="4dp"

或更改其值