Android 将TextView绘图表缩放到相同大小
我有一个使用gridview实现的菜单,其中每个单元格由一个带有可绘制图标的TextView组成。结果是屏幕上满是图标,每个图标下都有描述。大多数图标都是从以下资源加载的:Android 将TextView绘图表缩放到相同大小,android,textview,drawable,scaling,Android,Textview,Drawable,Scaling,我有一个使用gridview实现的菜单,其中每个单元格由一个带有可绘制图标的TextView组成。结果是屏幕上满是图标,每个图标下都有描述。大多数图标都是从以下资源加载的: Drawable img = getResources().getDrawable(R.drawable.people); textView.setCompoundDrawablesWithIntrinsicBounds(null, img, null, null); byte[] imageAsBytes = org.k
Drawable img = getResources().getDrawable(R.drawable.people);
textView.setCompoundDrawablesWithIntrinsicBounds(null, img, null, null);
byte[] imageAsBytes = org.kobjects.base64.Base64.decode(base64BitmapString);
Bitmap bmp = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);
Drawable img = new BitmapDrawable(parent.getResources(), bmp);
textView.setCompoundDrawablesWithIntrinsicBounds(null, img, null, null);
为了个性化菜单,从存储在数据库中的位图中加载一些图标,如下所示:
Drawable img = getResources().getDrawable(R.drawable.people);
textView.setCompoundDrawablesWithIntrinsicBounds(null, img, null, null);
byte[] imageAsBytes = org.kobjects.base64.Base64.decode(base64BitmapString);
Bitmap bmp = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);
Drawable img = new BitmapDrawable(parent.getResources(), bmp);
textView.setCompoundDrawablesWithIntrinsicBounds(null, img, null, null);
我遇到的问题是,从数据库加载的资源并不总是显示与从资源加载的图标相同的大小。在某些设备上,它们的大小相同,但在其他设备上则更小
怎样才能使所有的可拉丝件按比例缩放成相同的尺寸
所有图像都是100x100 png文件。Android SDK目标版本为8及以上
我正在编辑此文档以包含网格和网格单元的xml:
网格单元:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/label"
style="@style/ButtonGridCell"
android:layout_centerHorizontal="true"
android:drawablePadding="4dp"
android:gravity="center" >
</TextView>
带有网格的页面:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/PageLayout" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/homelogo"
style="@style/HomeLogo" />
<GridView
android:id="@+id/button_grid"
style="@style/ButtonGrid"
android:layout_below="@id/homelogo" >
</GridView>
</RelativeLayout>
</RelativeLayout>
风格:
<style name="ButtonGridBase">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_centerHorizontal">true</item>
<item name="android:horizontalSpacing">15dp</item>
<item name="android:verticalSpacing">18dp</item>
<item name="android:gravity">center</item>
<item name="android:stretchMode">columnWidth</item>
<item name="android:layout_margin">8dp</item>
<item name="android:fadeScrollbars">false</item>
</style>
<style name="ButtonGrid" parent="@ButtonGridBase">
<item name="android:columnWidth">100dp</item>
<item name="android:numColumns">auto_fit</item>
</style>
填补家长的空缺
包装内容
真的
15dp
18dp
居中
列宽
8dp
假的
100dp
自动配合
您应该加载正确缩放的图像。更多信息,请参见本手册:以及从drawable文件夹中获取的资源,它们位于哪个文件夹中?这些资源是从drawable文件夹中加载的。我没有为hdpi、mdpi和ldpi制作不同版本的图像。