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

Java コード

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 に答える