【问题标题】:Issue with alignment using TextInputLayout and Spinner使用 TextInputLayout 和 Spinner 的对齐问题
【发布时间】:2016-10-10 00:27:08
【问题描述】:

我遇到了 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。任何人都可以帮助我吗?提前致谢。

编辑:

这是我想要的:

我将 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>

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

【问题讨论】:

  • 检查我编辑的答案

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


【解决方案1】:

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

使用此代码:

<?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>

希望对你有帮助!!!

【讨论】:

  • 谢谢你的回答,但我想实现这个-> [链接] (imgur.com/a/ivuWg),有电话图标的行。
  • 你为什么不从一开始就这么说?
  • 对不起,如果我不清楚(英语不是我的母语),我告诉你电话图标只是为了让你在哪里看,我不想要图标,只是想要该行中的 EditText 和 Spinner 的行为。我编辑了我的问题以使其更清楚。也找到了解决方案,但我不确定是否对每个决定都有效。谢谢!
  • @helldawg13 edittext 和 spinner 将对齐?你确定吗?
  • @OvechkinPavel 试一试...如果是,请告诉我
猜你喜欢
  • 2018-10-01
  • 2017-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-02
  • 2019-09-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多