2013-01-11 42 views
0

我想将图像分配给TextView。我从Google中提取了一个.PNG文件,现在我试图将TextView的宽度和高度分配给我希望图像的大小。我希望它是一个小方形,但它变成了非常宽而短的矩形。在textview中制作一个固定大小的图像 - Android

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

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" /> 

Java代码

q1Image.setBackgroundResource(R.drawable.red_x); 

回答

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> 

这将会把图像视图,并在同一地点文本视图,使得相对布局的大小等于图像和文本视图大小相等到布局。然后它通过重力将文本居中

+0

我不想显示任何文本,只显示图像。 – Matt

+0

那你为什么要用TextView代替ImageView? –

+0

我猜是因为我是Android /移动编程的新手。我将切换到ImageView并给它一个去! – Matt

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; 
    } 

您可以指定高度和宽度得到它来调整根据您的requireements

相关问题