android:适合文本视图中 DrawableLeft 的高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22218486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android: fit height of DrawableLeft in a textView
提问by vipul mittal
I am trying to display a blue line next to a block of text, pretty much like this:
我试图在文本块旁边显示一条蓝线,非常像这样:
Here's my code:
这是我的代码:
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/blue_line" />
blue_line is a jpg file. a blue rectangle. it displays in its original size regardless of the text in the textview. how can i adjust its height dynamically according to the height of the text? like make it shorter when theres little amount of text and longer when there's more text....
blue_line 是一个 jpg 文件。一个蓝色矩形。无论文本视图中的文本如何,它都以其原始大小显示。如何根据文本的高度动态调整其高度?比如在文本量很少时让它更短,当文本更多时让它变长......
回答by vipul mittal
You can try doing it in code by setting bounds for the image
您可以通过为图像设置边界来尝试在代码中执行此操作
textView1.getViewTreeObserver()
.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Drawable img = ActivityName.this.getContext().getResources().getDrawable(
R.drawable.blue_line);
img.setBounds(0, 0, img.getIntrinsicWidth() * textView1.getMeasuredHeight() / img.getIntrinsicHeight(), textView1.getMeasuredHeight());
textView1.setCompoundDrawables(img, null, null, null);
textView1.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
回答by Droid Chris
The best way to do this is to wrap your drawable in an xml drawable file and set it as a drawable in your text view as follows:
最好的方法是将你的 drawable 包装在一个 xml drawable 文件中,并在你的文本视图中将其设置为一个 drawable,如下所示:
Drawable XML File:
可绘制的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:scaleType="fitXY"
android:src="@drawable/total_calories"/>
TextView in XML:
XML 格式的文本视图:
<TextView
android:id="@+id/title_total_cal"
style="@style/title_stats_textview"
android:drawableLeft="@drawable/total_calories_drawable"/>
回答by Hamid Shatu
Try as below...
尝试如下...
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/btndr" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/imageView" />
</RelativeLayout>
回答by Rethinavel
Simply, Keep your image as 9patch drawable.
简单地说,保持您的图像为 9patch 可绘制。
You can add android:drawableLeft="@drawable/checkmark"
to your textview. You can also set drawablePadding
to keep the textview organized.
您可以添加android:drawableLeft="@drawable/checkmark"
到您的文本视图。您还可以设置drawablePadding
保持文本视图有条理。
android:drawableLeft="@drawable/button_icon"
android:drawablePadding="2dip"
Here is the link to create 9patch drawable
这是创建 9patch drawable的链接
<TextView android:text="@string/txtUserName"
android:id="@+id/txtUserName"
android:layout_width="160dip"
android:layout_height="60dip"
android:layout_gravity="center"
android:drawableLeft="@drawable/button_icon"
android:drawablePadding="2dip"
/>
回答by Ali Mehrpour
You can draw a line with your desired height on drawable canvas and set as left drawable of your TextView. check this out:
您可以在可绘制画布上绘制一条具有所需高度的线,并将其设置为 TextView 的左可绘制。看一下这个:
public class FullHeightLineDrawable extends Drawable {
private Paint mPaint;
private int mHeight;
public FullHeightLineDrawable(int height) {
mPaint = new Paint();
mPaint.setColor(CompatUtils.getColor(R.color.colorAccent));
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(15);
mHeight = height;
}
@Override
public void draw(Canvas canvas) {
canvas.drawLine(0, -mHeight, 0, mHeight, mPaint);
}
@Override
public void setAlpha(int alpha) {
}
@Override
public void setColorFilter(ColorFilter colorFilter) {
}
@Override
public int getOpacity() {
return 0;
}
}
Usage:
用法:
final Drawable drawable = new FullHeightLineDrawable(getHeight());
mTextView.setTextColor(watchingColor);
mTextView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
回答by KyleZB
I abstract out this method. It works when the drawable is on the left of the TextView,dynamically scaling. If drawable is on the right side,this method doesn't work, needing to figure out why, but you can just directly use textView.setcompounddrawableswithintrinsicbounds()
with the right-size Drawable resource
我抽象出这个方法。当 drawable 位于 TextView 的左侧时,它可以动态缩放。如果drawable在右边,这个方法不起作用,需要弄清楚原因,但你可以直接使用textView.setcompounddrawableswithintrinsicbounds()
合适大小的Drawable资源
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
public static void ajustCompoundDrawableSizeWithText(final TextView textView, final Drawable leftDrawable, final Drawable topDrawable, final Drawable rightDrawable, final Drawable bottomDrawable) {
textView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
if(leftDrawable != null){
leftDrawable.setBounds(0, 0, (int)textView.getTextSize(), (int)textView.getTextSize());
}
if(topDrawable != null){
topDrawable.setBounds(0, 0, (int)textView.getTextSize(), (int)textView.getTextSize());
}
if(rightDrawable != null){
rightDrawable.setBounds(0, 0, (int)textView.getTextSize(), (int)textView.getTextSize());
}
if(bottomDrawable != null){
bottomDrawable.setBounds(0, 0, (int)textView.getTextSize(), (int)textView.getTextSize());
}
textView.setCompoundDrawables(leftDrawable, topDrawable, rightDrawable, bottomDrawable);
textView.removeOnLayoutChangeListener(this);
}
});
}
回答by guy.gc
I Created a class that extends TextView
, and resize the drawables in onPreDrawListener
.
我创建了一个扩展类TextView
,并在onPreDrawListener
.
public class MyTextView extends AppCompatTextView {
public MyTextView(Context context, AttributeSet attrs, int style) {
super(context, attrs, style);
fitCompoundDrawableToLineHeight();
}
private void fitCompoundDrawableToLineHeight() {
OnPreDraw.run(this, () -> {
final Drawable[] cd = getCompoundDrawables();
Arrays.stream(cd)
.filter(drawable -> drawable != null)
.forEach(d -> d.setBounds(0, 0, getLineHeight(), getLineHeight()));
setCompoundDrawables(cd[0], cd[1], cd[2], cd[3]);
});
}
}
// Convenience class that abstracts onPreDraw logic
public class OnPreDraw {
public static void run(View view, Runnable task) {
view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
if (!view.getViewTreeObserver().isAlive()) {
return true;
}
view.getViewTreeObserver().removeOnPreDrawListener(this);
task.run();
return true;
}
});
}
}
回答by Tomislav Brabec
Unfortunately, setBoundswas not working for me so I had to do a workaround.
不幸的是,setBounds对我不起作用,所以我不得不做一个解决方法。
// Turn wanted drawable to bitmap
Drawable dr = getResources().getDrawable(R.drawable.somedrawable);
Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
// I had a square image with same height and width so I needed only TextView height (getLineHeight)
int size = textView1.getLineHeight();
Drawable d = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, size, size, true));
textView1.setCompoundDrawables(d, null, null, null);
// Now we can set some spacing between text and image
textView1.setCompoundDrawablePadding(10);
It is not the best solution regarding performance because new bitmap is created, but still works.
这不是关于性能的最佳解决方案,因为创建了新的位图,但仍然有效。