【问题标题】:EditText wont display at the center of the TextInputLayoutEditText 不会显示在 TextInputLayout 的中心
【发布时间】:2018-07-22 22:56:04
【问题描述】:

无论我做什么,我的 EditText 都是这样的:

我的 XML:

<android.support.design.widget.TextInputLayout
    android:id="@+id/text_input_layout_search"               
    android:layout_width="wrap_content"
    android:layout_weight="1"
    android:layout_gravity="top"
    android:gravity="top"                
    android:layout_height="wrap_content">
    <EditText
         android:background="@null"
         android:textColor="@color/black"
         android:layout_centerInParent="true"
         android:gravity="top"
         android:layout_gravity="top"
         android:id="@+id/SearchText"
         android:textColorLink="@color/black"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:textColorHint="@color/normatext"
     android:inputType="text"/>
 </android.support.design.widget.TextInputLayout>

我在所有可能的变化中更改了gravitylayout_gravity 的所有可能值!有什么想法吗?

编辑: 完整版面:

 <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_marginTop="0dp"
android:paddingTop="6dp"
        android:paddingBottom="5dp"
        android:layout_margin="3dp"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_height="45dp">
        <Button
            android:layout_width="43dp"
            android:layout_height="wrap_content"
            android:background="@drawable/ham"
            android:id="@+id/hambtn"
            android:textColorHint="@color/black"
            android:layout_weight="0"
            />
        <android.support.design.widget.TextInputLayout
    android:id="@+id/text_input_layout_search"               
    android:layout_width="wrap_content"
    android:layout_weight="1"
    android:layout_gravity="top"
    android:gravity="top"                
    android:layout_height="wrap_content">
    <EditText
         android:background="@null"
         android:textColor="@color/black"
         android:layout_centerInParent="true"
         android:gravity="top"
         android:layout_gravity="top"
         android:id="@+id/SearchText"
         android:textColorLink="@color/black"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:textColorHint="@color/normatext"
     android:inputType="text"/>
 </android.support.design.widget.TextInputLayout>
        <Button
            android:layout_width="55dp"
            android:layout_weight="0"
            android:layout_height="match_parent"
            android:background="@drawable/my_button_bg"
            android:text="news"
            android:id="@+id/newsbtn"
         android:layout_marginLeft="8dp"
            android:layout_marginRight="6dp"
            android:paddingBottom="5dp"
            />
    </LinearLayout>

【问题讨论】:

  • 你的完整布局 xml 文件是什么?另外,您稍后在 java 中修改视图吗?
  • 好的,已编辑...
  • 想出了一些调查方法,尝试其中一些并更新?

标签: android-layout android-edittext


【解决方案1】:

我认为问题可能出在您最顶层的 LinearLayout 视图中。

尝试删除这些行:

android:layout_marginTop="0dp"
android:paddingTop="6dp"
android:paddingBottom="5dp"
android:layout_margin="3dp"

并添加这些行以确保根本没有填充和边距:

android:padding="0dp"
android:layout_margin="0dp"

如果它确实解决了问题,您可以继续阅读有关填充和边距的更多信息here。 如果不是,我会怀疑您的 TextInputLayout 会因为内部 EditText 要求它同时位于顶部和中心而感到困惑。删除“中心”部分(例如,从您的 EditText 中删除 - android:layout_centerInParent="true")并检查它是否解决了您的问题 :)

【讨论】:

  • 谢谢,这是罪魁祸首
  • 很高兴听到它有帮助 :)
【解决方案2】:

你能把 EditText 的背景标签去掉吗? 还提供您的完整布局文件以获得确切的解决方案。

【讨论】:

  • 我去掉了,只是白底还是一样
【解决方案3】:

以前从未遇到过这个问题,但我会明确确保父项中没有填充,子项中没有边距。即:

<android.support.design.widget.TextInputLayout
    ...
    android:padding="0dp" 
>

    <EditText
        ...
        android:layout_margin="0dp"
    />

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

【讨论】:

    猜你喜欢
    • 2016-09-10
    • 2015-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多