我试图在一个文本块旁边显示一条蓝线,非常像这样:
这是我的代码:
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/blue_line" />
blue_line是一个jpg文件。一个蓝色的矩形。无论textview中的文本如何,它都以原始大小显示。如何根据文字的高度动态调整其高度?比如当文字少的文字时缩短,当文字更多时更长......
答案 0 :(得分:29)
您可以通过设置图像的边界
尝试在代码中执行此操作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);
}
});
答案 1 :(得分:6)
执行此操作的最佳方法是将drawable包装在xml可绘制文件中,并在文本视图中将其设置为drawable,如下所示:
可绘制的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"/>
XML格式的TextView:
<TextView
android:id="@+id/title_total_cal"
style="@style/title_stats_textview"
android:drawableLeft="@drawable/total_calories_drawable"/>
答案 2 :(得分:4)
尝试如下......
<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>
答案 3 :(得分:2)
简单地说,保持您的图像为9patch drawable。
您可以在文本视图中添加android:drawableLeft="@drawable/checkmark"
。您还可以设置drawablePadding
以保持文本视图的有序性。
android:drawableLeft="@drawable/button_icon"
android:drawablePadding="2dip"
以下是创建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"
/>
答案 4 :(得分:1)
您可以在可绘制画布上绘制具有所需高度的线条,并将其设置为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;
}
}
用法:
final Drawable drawable = new FullHeightLineDrawable(getHeight());
mTextView.setTextColor(watchingColor);
mTextView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
答案 5 :(得分:0)
我抽象出这个方法。当drawable位于TextView的左侧时,它可以动态缩放。如果drawable在右侧,这种方法不起作用,需要找出原因,但您可以直接使用postprocess <- function(file) {
lines <- readLines(file)
include <- grep("^## ---.*purl = TRUE.*$", lines)
empty <- grep("^\\s*$", lines, perl = TRUE)
do_chunk <- function(start) {
start <- start + 1L
if (start > length(lines)) return()
## first empty line after start
end <- empty[empty >= start][1]
if (is.na(end)) end <- length(lines)
lines[start:end] <<- sub("^## ", "", lines[start:end])
}
lapply(include, do_chunk)
writeLines(lines, con = file)
}
与正确大小的Drawable资源
textView.setcompounddrawableswithintrinsicbounds()
答案 6 :(得分:0)
我创建了一个扩展TextView
的类,并在onPreDrawListener
中调整了drawable的大小。
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;
}
});
}
}
答案 7 :(得分:0)
不幸的是, 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);
这不是关于性能的最佳解决方案,因为创建了新的位图,但仍然有效。
答案 8 :(得分:-1)
您需要制作一个水平的XML布局,其中包含蓝线和右侧的textview。 比使用像项目的布局,并制作这些项目的ListView。像here之类的东西,但有点简单