android - 使用 TextInputLayout 和 Spinner 的对齐问题

标签 android android-layout android-spinner android-textinputlayout

我在使用 TextInputLayout 和 Spinner 时遇到对齐问题,我希望 Spinner 下划线与 TextInputLayout 内的 EditText 下划线对齐。这就是我正在做的:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="bottom">

    <android.support.design.widget.TextInputLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <EditText
            android:id="@+id/txt_discipline_code"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/lbl_input_discipline_code"/>
    </android.support.design.widget.TextInputLayout>

    <Spinner
        android:id="@+id/spnnr_color_discipline_register"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/Base.Widget.AppCompat.Spinner.Underlined">

    </Spinner>
</LinearLayout>

但是 Spinner 是一个 little bit below .任何人都可以帮助我吗?提前致谢。

编辑:

这是我想要的:The Y underline of the EditText equal to the Y underline of the Spinner

我将 Spinner layout_marginBottom 设置为 1.5dp 达到此对齐方式:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="bottom">

    <android.support.design.widget.TextInputLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <EditText
            android:id="@+id/txt_discipline_code"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Code"/>
    </android.support.design.widget.TextInputLayout>

    <Spinner
        android:id="@+id/spnnr_color_discipline_register"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/Base.Widget.AppCompat.Spinner.Underlined"
        android:layout_marginBottom="1.5dp">

    </Spinner>
</LinearLayout>

但恐怕这在其他尺寸不同的设备上无法正常工作。这是唯一的解决方案吗?

最佳答案

从你提到的你想要达到这个结果:

enter image description here

使用这段代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="bottom"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:baselineAligned="false">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/ic_launcher"
            android:id="@+id/imageView"
            android:padding="10dp"
            android:layout_weight="5" />

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <EditText
                android:id="@+id/name_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:hint="Name"/>
        </android.support.design.widget.TextInputLayout>
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/ic_launcher"
            android:id="@+id/imageView1"
            android:padding="10dp"

            android:layout_weight="1.1" />

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="-12dp"
            android:layout_marginStart="-12dp"
            android:layout_weight="0.6">
            <EditText
                android:id="@+id/code_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:hint="Code"/>
        </android.support.design.widget.TextInputLayout>

        <Spinner
            android:id="@+id/spnnr_color_discipline_register"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="wrap_content"/>
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/ic_launcher"
            android:id="@+id/imageView2"
            android:padding="10dp"
            android:layout_weight="5" />

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <EditText
                android:id="@+id/foo_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:hint="Foo"/>
        </android.support.design.widget.TextInputLayout>
    </LinearLayout>


</LinearLayout>

希望对您有所帮助!!!

关于android - 使用 TextInputLayout 和 Spinner 的对齐问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39940743/

相关文章:

javascript - 找不到 Phonegap 版本 3 phonegap.js

android - 如何验证android中的密码字段?

android-layout - 如何使用清除按钮制作可重复使用的 EditText?

android - WebView iframe 忽略 frameborder ="0"

java - 为 Android Studio 项目和 CI 添加 gradle 测试。没有找到测试

Android记事本练习1教程和Menu.FIRST

android - 滑动以在回收站 View android 中显示另一个隐藏的布局

安卓 : Spinner inside TabView Fragment replaces Tabs when implementing

java - 如何将按钮链接到微调器?

android - 从 LiveData(房间数据库)填充微调器