我有项目列表是一个TextView
; 并使用drawableleft&drawableright来塑造drawableLeft
.问题是,每当textview中的文本较大时,drawableleft和drawableleft都不会根据drawableRight
高度自动缩放.
是否可以在textview中缩放drawableleft和drawableright的高度?(我使用9补丁图片)
Sha*_*ahi 31
将您的资源包装在一个可绘制的文件中,该可绘制文件定义了您所需的大小,类似于:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/icon"
android:width="@dimen/icon_size"
android:height="@dimen/icon_size" />
</layer-list>
Run Code Online (Sandbox Code Playgroud)
之后,在你的textview标签中使用这个drawable
kir*_*ilv 26
这可能会帮助你.scaleX和scaleY有两个属性
下面的代码将缩小图像和文本的30%.因此,您必须使用许多"sp"增加字体大小,以便在重新调整大小(缩放)时,它将适合您喜欢的"sp".
例.如果我将字体设置为18,那么18%中的30%是5.4sp,粗略地说,这是我所针对的值,因为当它被缩放时,它将变为13sp
<TextView
android:textSize="18sp"
android:scaleX="0.7"
android:scaleY="0.7"
Run Code Online (Sandbox Code Playgroud)
最后要做的是设置CompundDrawable.
tview.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.xxx), null, null, null);
Run Code Online (Sandbox Code Playgroud)
我通过引入ScaleDrawable
并覆盖它来解决一个等效的用例.getIntrisicHeight()
,使其至少达到TextView
高度.的TextView.addOnLayoutChangeListener
部分,重新绑定需要Drawable
在TextView
尺寸变化可与API11 +
Drawable underlyingDrawable =
new BitmapDrawable(context.getResources(), result);
// Wrap to scale up to the TextView height
final ScaleDrawable scaledLeft =
new ScaleDrawable(underlyingDrawable, Gravity.CENTER, 1F, 1F) {
// Give this drawable a height being at
// least the TextView height. It will be
// used by
// TextView.setCompoundDrawablesWithIntrinsicBounds
public int getIntrinsicHeight() {
return Math.max(super.getIntrinsicHeight(),
competitorView.getHeight());
};
};
// Set explicitly level else the default value
// (0) will prevent .draw to effectively draw
// the underlying Drawable
scaledLeft.setLevel(10000);
// Set the drawable as a component of the
// TextView
competitorView.setCompoundDrawablesWithIntrinsicBounds(
scaledLeft, null, null, null);
// If the text is changed, we need to
// re-register the Drawable to recompute the
// bounds given the new TextView height
competitorView.addOnLayoutChangeListener(new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right,
int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
competitorView.setCompoundDrawablesWithIntrinsicBounds(scaledLeft, null, null, null);
}
});
Run Code Online (Sandbox Code Playgroud)
小智 8
希望这有帮助
Textview tv = (TextView) findViewById(R.id.tv_dummy)
int imageResource = R.mipmap.ic_image;
Drawable drawable = ContextCompat.getDrawable(context, imageResource);
int pixelDrawableSize = (int)Math.round(tv.getLineHeight() * 0.7); // Or the percentage you like (0.8, 0.9, etc.)
drawable.setBounds(0, 0, pixelDrawableSize, pixelDrawableSize); // setBounds(int left, int top, int right, int bottom), in this case, drawable is a square image
tv.setCompoundDrawables(
null, //left
null, //top
drawable, //right
null //bottom
);
Run Code Online (Sandbox Code Playgroud)
唯一可以接受的答案应该是照常使用带有scaleTypes的ImageView。hacky解决方法可以在Android不支持的TextView上缩放图像。按预期使用SDK。
我没有找到办法。但您显示的内容看起来像是一个项目列表。每个项目都是一个水平的 LinearLayout,imagesView 从左到右,文本在中心。图像尺寸执行 android:scaleType="fitXY" 将尺寸高度调整为文本视图高度。如果您不想使图像变形,请使用scaleType =“CENTER_INSIDE”。http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
<LinearLayout
android:layout_width="fill_parent"
android:id="@+id/HeaderList"
android:layout_gravity="top"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/backImg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:background="@color/blancotransless"
android:src="@drawable/header"
android:scaleType="fitXY" >
</ImageView>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/NameText"
android:text="The time of prayer"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:paddingLeft="4dp"
android:paddingTop="4dp"
/>
<ImageView
android:id="@+id/backImg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:background="@color/blancotransless"
android:src="@drawable/header"
android:scaleType="fitXY" >
</ImageView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
71365 次 |
最近记录: |