如何设置android:可绘制到左侧和顶部?

如何设置android:可绘制到左侧和顶部?,android,drawable,Android,Drawable,我只想将图标设置到我的文本视图的左上角。 这分别是我的代码和输出: <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/Content" android:drawableLeft="@drawable/icon" /> 输出: 但我想将我的图标设置在顶部和左侧,如下所示: 您可以使用set

我只想将图标设置到我的
文本视图的左上角。
这分别是我的代码和输出:

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

输出:

但我想将我的图标设置在顶部和左侧,如下所示:


您可以使用
setCompoundDrawablesWithIntrinsicBounds(int-left、int-top、int-right、int-bottom)

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


您可以使用
setCompoundDrawablesWithIntrinsicBounds(int-left、int-top、int-right、int-bottom)

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

使用下面的代码

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

使用下面的代码

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

为此,您必须像这样使用单独的imageView,并且在textview字段中,您必须添加一行代码,android:layout\u 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>

为此,您必须像这样使用单独的imageView,并且在textview字段中,您必须添加一行代码,android:layout\u 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>

试试这个

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

试试这个

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

您可以通过创建一个包裹您的可拉深部件的自定义可拉深部件,将复合可拉深部件与顶部(或底部)对齐

用法

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);
自定义重力复合绘图类:

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

您可以通过创建包裹您的可拉伸材料的自定义可拉伸材料,将复合可拉伸材料与顶部(或底部)对齐

用法

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);
自定义重力复合绘图类:

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

此代码将帮助您查看所附的图像

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


此代码将帮助您查看所附图像

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


如此有效的解决方案中没有一个来自我。最后我是这样做的:

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

如此有效的解决方案没有一个来自我。最后我是这样做的:

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


使用单个视图不可能很容易做到这一点,您可能必须为图像使用Relativelayout和单独的imageviewit@jigarsavaliya你能解释一下吗?单视图不太容易,你可能需要使用Relativelayout和一个单独的图像视图it@jigarsavaliya你能解释更多吗?看起来像完美答案无法接近我的解决方案,因为当我设置图标的顶部和左侧值时,它会复制此图标的两个版本…如果你给xml文件看起来像完美答案无法接近我的解决方案,因为当我为我的图标设置Top和Left值时,它会复制这个图标的两个版本…如果您为您的feedback@DeepakRanathnx@komalakhanks为您的feedback@DeepakRanaNested布局不利于性能。相反,考虑使用相对论。或者是一个网格视图,一个表格布局。。。任何允许你不在彼此内部嵌套线性布局的东西。我会把ImageView(实际上是一个)放在前面。然后将TexView相对于它放置。但是你会得到@deepakrana的答案。嵌套布局不利于性能。相反,考虑使用相对论。或者是一个网格视图,一个表格布局。。。任何允许你不在彼此内部嵌套线性布局的东西。我会把ImageView(实际上是一个)放在前面。然后将TexView相对于它放置。但你会得到@deepakrana的答案。哦,不。。。再一次!嵌套布局不利于性能。相反,考虑使用相对论。或者是一个网格视图,一个表格布局。。。任何允许你不在彼此内部嵌套线性布局的东西。哦,不。。。再一次!嵌套布局不利于性能。相反,考虑使用相对论。或者是一个网格视图,一个表格布局。。。允许您不将线性布局嵌套在彼此内部的任何内容。从此处复制:从此处复制: