简体   繁体   English

如何设置 android:drawable to left and top?

[英]How to set android:drawable to left and top?

I just wanted to set icon to the top and left corner of my TextView .我只是想将图标设置在TextView的左上角和左上角。 This is my code and the output respectively:这是我的代码和 output 分别:

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

Output: Output:

输出

but I want to set my icon to the top and left like this one:但我想将我的图标设置在顶部和左侧,如下所示:

在此处输入图像描述

For this purpose you have to take the seperate imageView like this and in textview field you have to add one line code that android:layout_toRightOf="@+id/imageView": For better understanding see following code:为此,您必须像这样使用单独的 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>

You can use setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom)您可以使用setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom)

(For better Understand refer this link:) (为了更好地理解,请参阅此链接:)

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

Use below code;使用下面的代码;

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

try this试试这个

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

You can align a compound-Drawable to the top (or bottom) by creating a custom Drawable that wraps your Drawable.您可以通过创建一个包含 Drawable 的自定义 Drawable 来将复合 Drawable 对齐到顶部(或底部)。

Usage用法

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

Custom GravityCompoundDrawable class:自定义 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();
 }
}

This code will help you and see the attached image.此代码将帮助您并查看附加的图像。

  <?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 https://i.stack.imgur.com/hLUGT.png

None of the solutions from SO worked form me. SO的所有解决方案都没有对我有用。 In the end I did it like this:最后我是这样做的:

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()
                    }
                }
            }
        }
    }
}

If you want to resolve it using compound drawable, you should create a custom textview and use it.如果您想使用复合可绘制来解决它,您应该创建一个自定义 textview 并使用它。

import android.content.Context
import android.graphics.Canvas
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView


class TopAlignCompoundDrawableTextView: AppCompatTextView {
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(
        context,
        attrs,
        defStyle
    )

    override fun dispatchDraw(canvas: Canvas?) {
        super.dispatchDraw(canvas)
        val centerTextView = height / 2

        val drawableLeft = compoundDrawables[0]
        drawableLeft?.let {
            val topDrawable = it.intrinsicHeight/2 - centerTextView
            it.setBounds(
                0,
                topDrawable,
                it.intrinsicWidth,
                it.intrinsicHeight + topDrawable
            )
        }

        val drawableRight = compoundDrawables[2]
        drawableRight?.let {
            val topDrawable = it.intrinsicHeight/2 - centerTextView
            it.setBounds(
                0,
                topDrawable,
                it.intrinsicWidth,
                it.intrinsicHeight + topDrawable
            )
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM