java - Android - Editbox Drawable 左边没有正确填充

标签 java android android-layout

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

我希望 EditBox 看起来像这样(来自 Instagram 应用):

enter image description here

请注意,图片已正确居中,并且图片的上/右/左/下内边距看起来都一样。右边的文字以图像的大小为界(在我画的蓝线之间)

当我尝试做同样的事情时,我的 EditText View 看起来像这样:

enter image description here

我还将这个多行编辑文本框的行号设置为三,但这并没有帮助修复框的大小。图片的顶部/底部有一个很大的填充,我不知道它来自哪里。这是我的代码:

布局

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

圆角可绘制

<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 布局相同?

谢谢

最佳答案

有两种方法可以做到这一点。

  1. 在线性布局/相对布局中使用带有 ImageView 和 TextView 的自定义布局。无论文本和图像大小如何,它都会使您的列表项统一。
  2. 将 drawable 与 EditText 结合使用是一个坏主意。当您需要使用默认重力在边缘上显示图像时,请使用 drawableLeft 或 Right。与 EditText 关联的可绘制对象的边距和填充是一项繁琐的任务。

关于java - Android - Editbox Drawable 左边没有正确填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20439426/

相关文章:

android - BottomSheetDialogFragment 中 ADJUST_RESIZE 的不同行为

android - 找不到 RecyclerView 类 android.support.v7.recyclerview.R$styleable

java - Intellij IDEA在引用Function::andThen时无法解析 'andThen'函数式接口(interface)方​​法

java - 如何找出序列化 Java 对象的 serialVersionUID?

java - 如何在Java Android Studio中多次运行代码

java - 是否可以在不导入 android ScrollView 类的情况下创建 scrollview 对象?

java - 在 Java 中向 Thread 类添加函数

java - Java InputStream(或 CsvMapper)中的总行数

android - 将关键字和定位信息发送到 AdMob 以获得更好的广告

android - 没有空间时换行右对齐 View