将基线与TextView进行对齐,TextView包含在TextInputLayout中

Adi*_*que 10 android android-layout android-textinputlayout android-percent-library

我想将TextView"Deficient"的基线与EditText"10000"的基线对齐.他们都在里面<android.support.percent.PercentRelativeLayout>

在此输入图像描述

我尝试了以下,但无法让它工作.

<android.support.percent.PercentRelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInputLayout_soil_nitrogen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        app:layout_widthPercent="63%">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter N of soil"
            tools:text="10000" />

    </android.support.design.widget.TextInputLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@id/textInputLayout_soil_nitrogen"
        android:layout_toRightOf="@id/textInputLayout_soil_nitrogen"
        app:layout_widthPercent="37%"
        tools:text="Deficient" />

</android.support.percent.PercentRelativeLayout>
Run Code Online (Sandbox Code Playgroud)

给TextView类型提供20dp的顶部填充可以解决问题,但是对齐它们的基线会很不错.在这种情况下,这甚至可能吗?

我删除了textAppearance和id属性,顺便说一句.

Jon*_*ona 11

老问题,但这是我的解决方案.

基本上TextInputLayout没有为它的父级提供基线值.我们需要EditText通过扩展来管道正确的基线TextInputLayout.这对我有用,但是,我不确定基线会因为其他事件而改变TextInputLayout.

public class CTextInputLayout extends TextInputLayout {
    public CTextInputLayout(Context context) {
        super(context);
    }

    public CTextInputLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public int getBaseline()
    {
        EditText editText = getEditText();
        return editText.getPaddingTop() + editText.getBaseline();
    }
}
Run Code Online (Sandbox Code Playgroud)

我假设PercentRelativeLayout支持基线对齐.否则,你可以在一个基线对齐中包裹CTextInputLayoutTextView内部RelativeLayout.


小智 1

请替换下面的代码行

 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@id/textInputLayout_soil_nitrogen"
        android:layout_toRightOf="@id/textInputLayout_soil_nitrogen"
        app:layout_widthPercent="37%"
        tools:text="Deficient" />
Run Code Online (Sandbox Code Playgroud)

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textInputLayout_soil_nitrogen"
        android:layout_toEndOf="@id/textInputLayout_soil_nitrogen"
        android:layout_toRightOf="@id/textInputLayout_soil_nitrogen"
        tools:text="Deficient" />
Run Code Online (Sandbox Code Playgroud)

希望它能帮助你!

  • 不行,这样textView的基线和editText的线(上图中紫色的线)在同一水平线上,这不是我想要的。**我想对齐它们两个的基线。**我认为这是不可能的,因为 TextView 位于 TextInputLayout 内。我想我只能添加 20dp 的顶部内边距。我希望这里有人知道如何实现这一目标。不过还是谢谢你的回答。祝你有美好的一天。:) (3认同)