Android - 编辑框左侧的Drawable未正确填充间距

3

我有一个编辑框,在其中定义了一个android:drawableLeft属性。

我期望的是,编辑框看起来像这样(从Instagram应用程序中):

enter image description here

请注意,图像已正确居中,并且图像的上/右/左/下填充似乎都相同。右侧的文本被图像的大小所限制(在我绘制的蓝线之间)

当我尝试做同样的事情时,我的EditText视图如下:

enter image description here

我还将此多行EditText框的行号设置为三,但这并没有帮助解决框的大小。有一个大的填充在图像的顶部/底部,我不知道它来自哪里。这是我的代码:

布局

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dp"
    android:weightSum="1" >

    <EditText
        android:id="@+id/new_post_edittext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/rounded_edittext"
        android:drawableLeft="@drawable/ic_launcher"
        android:ems="10"
        android:gravity="top|left"
        android:hint="@string/compose_card_hint"
        android:lines="3"
        android:padding="5dip"
        android:drawablePadding="5dp"
        android:textColor="#999999" >

        <requestFocus />
    </EditText>

</LinearLayout>

圆角Drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="5dp"
android:shape="rectangle" >

<solid android:color="#bbbbbb" />

<stroke
    android:width="1dp"
    android:color="#2f6699" />

<corners
    android:bottomLeftRadius="5dp"
    android:bottomRightRadius="5dp"
    android:topLeftRadius="5dp"
    android:topRightRadius="5dp" />

</shape>

还有动态设置图像的代码:

Drawable img = this.getResources().getDrawable( R.drawable.sample_img );
    img.setBounds( 0, 0, 200, 200);

    EditText et = (EditText) findViewById(R.id.new_post_edittext);

    et.setCompoundDrawables( img, null, null, null );

我该如何重新调整布局,使其看起来与我上面展示的 Instagram 布局相同?

谢谢。


仅需将图像与EditText分离,使用ImageView和EditText,并用相对布局包装它。 - EvZ
遇到了相同的问题。尝试使用9patch但是无用。 - Trung Nguyen
android:lines="3" 导致了底部和顶部的填充。尝试将其设置为1以查看差异。我认为没有简单的解决方法,所以建议您遵循 @EvZ 的建议。 - chjarder
2个回答

0

你可以有两种方法来实现它。

  1. 使用自定义布局,在LinearLayout/RelativeLayout中使用ImageView和TextView。这将使您的列表项无论文本和图像大小都是统一的。
  2. 在EditText中使用drawable与lines的组合是一个不好的想法。当您需要在默认重力边缘显示图像时,请使用drawableLeft或Right。与EditText相关联的drawables的Margin和Padding是一项繁琐的任务。

0

你尝试过将 android:gravity="top|left" 改为 android:gravity="center_vertical" 吗?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接