【问题标题】:Custom compound view extended from RelativeLayout. Incorrect positioning of child views从 RelativeLayout 扩展的自定义复合视图。子视图的定位不正确
【发布时间】:2019-05-02 17:04:55
【问题描述】:

我想制作包含 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 效果很好,看起来和计划的一样。为了制作复合视图,我删除了&lt;/RelativeLayout&gt; 标记并将其包装到&lt;/merge&gt; 中(我不应用代码,因为除了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 扩展视图足以正确定位。所以我需要你的帮助来定义我哪里出错了。如何正确定位元素?

附:将RelativeLayout替换为merge标签是为了优化视图的绘制并避免不必要的嵌套布局

【问题讨论】:

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


    【解决方案1】:

    首先你不需要拥有

    android:layout_above="@id/editTextHint"
    

    并添加

    android:layout_gravity="center_vertical"
    

    编辑TextHint和textViewContent

    你也可能应该删除

    android:layout_marginEnd="4dp"
    

    或改变一点它的价值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-16
      • 2014-09-02
      • 2023-02-02
      • 2012-04-06
      相关资源
      最近更新 更多