0

我正在尝试将图像分配给 TextView。我从 Google 提取了一个 .PNG,现在正尝试将 TextView 的宽度和高度分配给我想要的图像大小。我希望它是一个小正方形,但它变成了非常宽和短的矩形。

我对这一切都错了吗?下面是代码。

xml代码

    <TextView
        android:id="@+id/q1Image"
        android:layout_width="1dp"
        android:layout_height="10dp"
        android:layout_weight=".1"
        android:textSize="8sp"
        android:gravity="center_horizontal" />

爪哇代码

q1Image.setBackgroundResource(R.drawable.red_x);
4

3 回答 3

1

你想做什么 - 让文本在图像上居中?如果是这样,你需要做一些不同的事情

<RelativeLayout
  android:layout_width=wrap_content
  android:layout_height=wrap_content>
    <TextView 
        android:layout_width=fill_parent
        android:layout_height=fill_parent
        android:gravity:"center" />
    <ImageView
        android:layout_width=wrap_content
        android:layout_height=wrap_content
        android:scaleType="centerInside" />
</RelativeLayout>

这会将图像视图和文本视图放置在同一位置,使相对布局的大小与图像相同,而文本视图的大小与布局相同。然后它通过重力使文本居中

于 2013-01-11T17:35:16.433 回答
0
public Drawable getDrawable(String source) {

        Log.i(" get drawable mathod ", "");
        Bitmap B = BitmapFactory.decodeResource(getResources(),
                R.drawable.bone);
        BitmapDrawable BD = new BitmapDrawable(B);

        BD.setBounds(0, 0, B.getWidth(), B.getHeight());

        return BD;
    }

您可以指定高度和宽度以根据您的要求进行调整

于 2013-01-11T17:32:01.263 回答
0

请查看TextView的文档。

TextView 具有以下功能:

setCompoundDrawablesWithIntrinsicBounds(int,int,int,int) setCompoundDrawablesRelativeWithIntrinsicBounds(int,int,int,int)
和其他可能对您有帮助的函数。

于 2013-01-11T17:34:49.130 回答