【问题标题】:How to set android:drawable to left and top?如何将 android:drawable 设置为左​​侧和顶部?
【发布时间】:2017-08-08 10:12:59
【问题描述】:

我只是想将图标设置在我的TextView 的左上角。 这是我的代码和输出:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Content"
    android:drawableLeft="@drawable/icon" />

输出:

但我想像这样将我的图标设置在顶部和左侧:

【问题讨论】:

  • 使用单一视图不容易做到这一点,您可能必须使用相对布局和单独的图像视图。
  • 使用重力并设置它
  • @jigarsavaliya 你能解释更多吗?

标签: android drawable


【解决方案1】:

为此,您必须像这样使用单独的 imageView,并且在 textview 字段中您必须添加一行代码 android:layout_toRightOf="@+id/imageView": 为了更好地理解,请参阅以下代码:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:background="@mipmap/ic_launcher"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
      android:layout_toRightOf="@+id/imageView"
        android:text="this si fodfjkhsdhaffjsdfasdfjhsdfhjsdhfjhsdfhsdhfhdsjfhsdjhfjhdsfhsdhfjhsdjhfjsdhfjhsdjfhjsdhfjhsdjfhjdshfsdjhfjsdhfsdkjhfjsdhfjhsdjfhjsdhjfhsdjhfjsdhfjhjsdhfjsdhjfhsdjf"
        />

</RelativeLayout>

【讨论】:

  • thnx @komalakhani
  • 感谢您的反馈@DeepakRana
【解决方案2】:

使用下面的代码;

<TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/Content"
   android:drawableTop="@drawable/def"
   android:drawableLeft="@drawable/icon" />

【讨论】:

    【解决方案3】:

    您可以通过创建一个包裹您的 Drawable 的自定义 Drawable 来将复合绘图与顶部(或底部)对齐。

    用法

    GravityCompoundDrawable gravityDrawable = new GravityCompoundDrawable(innerDrawable);
    // NOTE: next 2 lines are important!
    innerDrawable.setBounds(0, 0, innerDrawable.getIntrinsicWidth(), innerDrawable.getIntrinsicHeight());
    gravityDrawable.setBounds(0, 0, innerDrawable.getIntrinsicWidth(), innerDrawable.getIntrinsicHeight());
    textView.setCompoundDrawables(gravityDrawable, null, null, null);
    

    自定义 GravityCompoundDrawable 类:

    public class GravityCompoundDrawable extends Drawable {
    
    // inner Drawable
    private final Drawable mDrawable;
    
    public GravityCompoundDrawable(Drawable drawable) {
        mDrawable = drawable;
    }
    
    @Override
    public int getIntrinsicWidth() {
        return mDrawable.getIntrinsicWidth();
    }
    
    @Override
    public int getIntrinsicHeight() {
        return mDrawable.getIntrinsicHeight();
    }
    
    @Override
    public void draw(Canvas canvas) {
        int halfCanvas= canvas.getHeight() / 2;
        int halfDrawable = mDrawable.getIntrinsicHeight() / 2;
        // align to top
        canvas.save();
        canvas.translate(0, -halfCanvas + halfDrawable);
        mDrawable.draw(canvas);
        canvas.restore();
     }
    }
    

    【讨论】:

    【解决方案4】:

    你可以使用setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom)

    (为了更好地理解,请参阅此链接:)

    https://developer.android.com/reference/android/widget/TextView.html#attr_android:drawableTop

    【讨论】:

    • 看起来完美的答案
    • 无法接近我的解决方案,因为当我将 Top 和 Left 值设置为我的图标时,它会复制此图标的两个版本...
    • 如果你给你的xml文件
    【解决方案5】:

    此代码将帮助您并查看所附图片。

      <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <LinearLayout
            android:id="@+id/llMain"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="1">
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.10"
                android:orientation="vertical">
    
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/ic_launcher" />
    
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/ic_launcher" />
    
            </LinearLayout>
    
            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.90"
                android:text="It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using" />
        </LinearLayout>
    </LinearLayout>
    

    https://i.stack.imgur.com/hLUGT.png

    【讨论】:

    • 哦,不……又一次!嵌套布局不利于性能。考虑改用RelativeLayout。或者一个 GridView、一个 TableLayout,......任何允许你不将 LinearLayouts 嵌套在另一个内部的东西。
    【解决方案6】:

    试试这个

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="hello world"
        android:layout_toEndOf="@+id/imageView" />
    
    
    
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_error" />
    
    
    </RelativeLayout>
    

    【讨论】:

    • 嵌套布局不利于性能。考虑改用RelativeLayout。或者一个 GridView、一个 TableLayout,......任何允许你不将 LinearLayouts 嵌套在另一个内部的东西。
    • 我之前会放 THE ImageView(实际上是 ONE)。然后把 TexView 相对于它。但是你会得到@deepakrana 的答案。
    【解决方案7】:

    SO 的任何解决方案都对我不起作用。最后我是这样做的:

    class MyTextView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null
    ) : AppCompatTextView(context, style) {
        private val leftDrawable = ContextCompat.getDrawable(context, R.drawable.checkmark)
    
        override fun onDraw(canvas: Canvas?) {
            super.onDraw(canvas)
            setBulletPoint(compoundDrawables[0], canvas)
        }
    
        private fun setBulletPoint(drawableLeft: Drawable?, canvas: Canvas?) {
            if (!TextUtils.isEmpty(text)) {
                leftDrawable?.let { drlft ->
                    if (lineCount == 1) {
                        setCompoundDrawablesWithIntrinsicBounds(drlft, null, null, null)
                    } else {
                        val buttonWidth = drlft.intrinsicWidth
                        val buttonHeight = drlft.intrinsicHeight
                        val topSpace = abs(buttonHeight - lineHeight) / 2
               
                        drlft.setBounds(0, topSpace, buttonWidth, topSpace + buttonHeight)
                        canvas?.apply {
                            save()
                            drlft.draw(canvas)
                            restore()
                        }
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-04-05
      • 1970-01-01
      • 1970-01-01
      • 2011-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-07
      • 2016-07-10
      相关资源
      最近更新 更多