【问题标题】:How to remove EditText drawable padding?如何删除 EditText 可绘制填充?
【发布时间】:2015-03-28 08:54:03
【问题描述】:

我浪费了最后一个小时试图弄清楚如何在一个简单的 EditText 中摆脱这种填充:

我想要的只是将输入与其余内容对齐,就像漂亮的design guide 所说的那样。

布局再简单不过了:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context="com.actinarium.tiomantas.SetupChallengeFragment">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:orientation="vertical">

        <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/new_challenge_name"/>

        <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:hint="@string/new_challenge_name_hint1"
                android:singleLine="false"/>

    </LinearLayout>

</ScrollView>

我尝试将 EditText 上的填充设置为 0dp — 文本移动但行保持原样。奇怪的是,我找不到任何关于这个问题的信息,好像没有人注意到一样。

【问题讨论】:

  • P.S.有些东西告诉我,就像 Android 中的其他任何东西一样,即使对于这个微不足道的事情,我也必须制作自己的可绘制对象......我只是想知道为什么他们的原生小部件不遵循自己的设计规范......

标签: android android-layout android-edittext


【解决方案1】:

我认为该行是 EditText 的背景图像,因此更改填充不会影响它。如果您愿意,您可以创建一个没有填充的自定义背景可绘制对象。

您可以做的另一个技巧是在 EditText 中添加一个负边距:

        <EditText
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_marginLeft="-4dp"
             android:hint="@string/new_challenge_name_hint1"
             android:singleLine="false"/>

【讨论】:

  • 查看了联系人应用程序。似乎谷歌开发人员遵循了这条路,只是解释了过多的 4 次下跌。老实说,我认为会有更好的解决方案。在 Fit 中,他们只是保持原样
  • 是的,我同意,应该有更好的方法。
  • 完美主义者很糟糕 :) 现在我担心当他们推出一种新的设计语言时这会打破 :)
  • 应该有一些适当的方法来处理它,他们可能会在 EditText 中保留一个属性
【解决方案2】:

尝试为 textView 提供“4dp”的填充。使其与 editText 填充匹配。

【讨论】:

  • 我考虑过这个。我不完全确定这种解决方法不会破坏棒棒糖之前的用户界面(我最不想要的是有很多特定于版本的 XML)
  • 我同意你的看法。但这是一个小工作。我检查了使用 4.4.2 在我的设备中添加填充和使用 5.0 的模拟器添加填充的结果,它似乎工作正常。
  • 刚刚查看了棒棒糖上的联系人应用程序。似乎开发人员只是考虑了过多的 4dp 并缩小了边距。天哪……
【解决方案3】:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.actinarium.tiomantas.SetupChallengeFragment">
<LinearLayout
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="jhjkhkhkjkljlkjkljlkj"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:hint="hjhjhjkhjkhjkhkjhoiuyiyirehyiohj"
            android:singleLine="false"/>
    </LinearLayout>
</LinearLayout>
</ScrollView>

【讨论】:

  • 您能否解释一下为什么您所做的更改将解决问题 - 鉴于您的解决方案增加了布局深度/复杂性。
猜你喜欢
  • 1970-01-01
  • 2015-11-11
  • 1970-01-01
  • 2014-07-23
  • 1970-01-01
  • 2012-07-23
  • 1970-01-01
  • 2020-07-21
  • 2017-11-20
相关资源
最近更新 更多