【问题标题】:Android EditText have always padding from bottomAndroid EditText 总是从底部填充
【发布时间】:2013-01-29 17:52:48
【问题描述】:

这是我的 ListView XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+list/list_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/defaultbg"
        android:drawSelectorOnTop="false" />

    <ImageButton
        android:id="@+list/filter_button"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:background="@android:color/transparent"
        android:contentDescription="@string/SmallLogo"
        android:scaleType="fitXY"
        android:src="@drawable/filter" />

    <!-- empty view -->

    <TextView
        android:id="@+list/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/defaultbg"
        android:gravity="center"
        android:text="@string/whos_around_empty_text"
        android:textAppearance="@android:style/TextAppearance.Medium"
        android:textColor="@android:color/white"
        android:textStyle="bold" />

    <EditText
        android:id="@+list/filter"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:hint="@string/search"                
        android:visibility="visible"/>
</RelativeLayout>

EditText 位于视图的底部,但由于某种原因它并不紧贴底部..

仔细检查时,我发现布局正好在底部 - 但内部的白色背景并没有覆盖整个布局(有点像 ImageView 大小大于布局大小并且您使用 scaleType 时) ..

EditText 紧贴底部对我来说真的很重要......有什么想法吗??

更新:添加屏幕截图:

如您所见 - editText 白色背景的底部未与屏幕底部对齐...尽管布局本身已对齐

【问题讨论】:

  • android:layout_below="@+id/list_view"
  • 那不行……它消失了。
  • 顺便说一句 - 将其与顶部对齐时 - 效果很好
  • 您可以尝试使用 android:layout_marginBottom="0dip" 将下边距设置为 0
  • 正如我所说 - 布局很紧凑......问题是布局内部有某种背景颜色的填充......我将在问题中添加图片

标签: android android-edittext


【解决方案1】:

在 Android 中,所有小部件都具有与其他视图相比的一些内边距和边距,以提高可见性。 每个小部件都有 4 dp 边距和 8 dp 填充。如果您不希望这样,您可以创建自定义视图。

现在,尝试在底部使用负像素边距。

android:layout_marginBottom="-4dp"

在我的布局上工作得很好

【讨论】:

  • 感谢您的回答。首先,正如您在我的屏幕截图中看到的那样,问题不在于边距。它可能是填充。其次,我尝试将其更改为 android:paddingBottom="-8dp" 但没有发生任何事情
  • @Asaf Nevo 我不是说将填充设置为 -8dp,而是说设置边距。看看代码先生。
  • 我确实试过了,它没有任何改变...可能与 layout_alignParentBottom="true" 有关吗?
【解决方案2】:

试试这个,我做过类似的屏幕。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"

    android:text="@string/whos_around_empty_text"
    android:background="@drawable/defaultbg"
    android:textAppearance="@android:style/TextAppearance.Medium"
    android:textColor="@android:color/white"
    android:textStyle="bold" />

<RelativeLayout
    android:id="@id/relative"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" >

    <ImageButton
        android:id="@+list/filter_button"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentRight="true"
        android:background="@android:color/transparent"
        android:contentDescription="Smalllogo"
        android:scaleType="fitXY"
        android:src="@drawable/filter" />

    <!-- empty view -->

    <EditText
        android:id="@+list/filter"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:hint="Search"
        android:visibility="visible" />
</RelativeLayout>

<ListView
    android:id="@+id/list_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/defaultbg"
    android:layout_above="@id/relative"
    android:layout_below="@id/empty"
    android:drawSelectorOnTop="false" />

【讨论】:

  • 请说明你做了什么样的改变?
  • 它确实像你提到的那样工作,但我希望 ImageButton 和 EditText 漂浮在 ListView 或 EmptyTextView 上。不要在它下面
  • 假设ListView没有任何项目意味着EditText应该出现在底部。
  • 它确实出现了,但是您的代码 - 如屏幕截图所示 - 正在将我的编辑文本定位在当前视图(ListView 或 EmptyTextView)下。我希望它浮在上面
猜你喜欢
  • 1970-01-01
  • 2017-09-13
  • 1970-01-01
  • 2019-12-25
  • 1970-01-01
  • 1970-01-01
  • 2017-05-18
  • 2015-03-22
  • 2020-06-12
相关资源
最近更新 更多