对齐图像顶部TextView的顶部 [英] Align top of image to top of TextView

查看:860
本文介绍了对齐图像顶部TextView的顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个布局图像的顶部对齐,以这样一个TextView的顶部:

I want to create a layout that aligns the top of an image to the top of a TextView like this:

---------  Text text text text text text text
| Image |  text text text text text text text
---------  text text text text text text text
           text text text text text text text
           text text text text text.

我试图通过设置这样安卓drawableLeft 来我的形象,但中心的图像垂直:

I tried doing this by setting android:drawableLeft to my image, but that centers the image vertically:

           Text text text text text text text
---------  text text text text text text text
| Image |  text text text text text text text
---------  text text text text text text text
           text text text text text.

这可能只是一个TextView的还是我需要创建一个包含一个TextView和ImageView的?一个RelativeLayout的

Is this possible with just a TextView or do I need to create a RelativeLayout containing a TextView and an ImageView?

下面是我的TextView的XML布局,让不正确的布局:

Here is the XML layout for my TextView that gives the incorrect layout:

<TextView
    android:gravity="top"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/image"
    android:drawablePadding="8dp"
    android:text="@string/text" />

Android的:重力=顶属性似乎只影响文本,而不是绘制

The android:gravity="top" attribute only seems to affect the text, not the drawable.

推荐答案

您可以通过创建一个自定义绘制对象,它包装你的绘制对象对齐复合抽拉顶端(或底部),然后处理您的自定义绘制对象的绘制通过覆盖方法的OnDraw(画布)

You can align a compound-Drawable to the top (or bottom) by creating a custom Drawable that wraps your Drawable, and then manipulate the drawing of your custom Drawable by overriding the method onDraw(Canvas).

下面示例是可能的最简单的例子。这会将图像顶端,但是你也可以把它调整到下方的TextView的,向左或向右实现在的OnDraw(画布)所需的逻辑 - 方法。您可能还需要建立在的OnDraw(Canvas)的保证金,使您的设计实现像素完美。

The sample below is the simplest possible example. This aligns the image to the top, but you can also make it align to the bottom, left or right of the TextView by implementing the required logic in the onDraw(Canvas)-method. You might also want to build in a margin in the onDraw(Canvas), to make your design implementation pixel perfect.

使用范例:

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());
mTextView.setCompoundDrawables(gravityDrawable, null, null, null);

样品code:

Sample code:

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

这篇关于对齐图像顶部TextView的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆