3

以下のようなxmlファイルがあり、これを使用して背景を設定しますTextview

row.xml

      <?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle">
      <gradient android:endColor="#CCCCCC" android:startColor="#CCCCCC"
      android:angle="270" />
      <stroke android:width="1dp" android:color="#999999" />
      <corners android:bottomRightRadius="0dp"
      android:bottomLeftRadius="0dp" android:topLeftRadius="0dp"
      android:topRightRadius="0dp" /></shape>

上記のXmlは、以下のようにmain.xmlでTextViewの背景として設定します。

main.xml

<TextView
android:id="@+id/rowtext3"
android:text="Availablity"
android:layout_height="25px"
android:layout_width="60px"
android:textSize="10px"
android:textStyle="bold"
android:textColor="@color/black"
android:gravity="center"
android:background="@drawable/row"
/>

しかし、これをXmlではなくコードから実行したいと思います。フォント、幅、高さ、フォントなど、Xmlで行ったすべてのことをコードを介して動的に行いましたが、Xmlファイルで言及した背景を設定できません。main.xmlで背景をXMLとして設定する方法と同様に、Xmlファイルのコンテンツを背景としてtextviewに設定するにはどうすればよいですか。

私がこのようにしたコードでは:

    t1=new TextView(this); <br>
    t1.setText(ed1.getText()); <br>
    t1.setHeight(25); <br>
    t1.setWidth(60); <br>
    t1.setTextSize(10); <br>

しかし、背景を設定する方法、つまりXMLコンテンツを背景として設定する方法が見つかりませんでしたか?
誰かがこの問題を整理するのを手伝ってくれますか?
前もって感謝します、

4

2 に答える 2

7

あなたが探している方法はですsetBackgroundDrawable(Drawable d)

これにより、指定されたDrawableを使用して背景が設定されます。したがって、次のようになります。

TextView t1 = (TextView) findViewById(R.id.rowtext3);
t1.setBackgroundDrawable(row);
于 2010-10-10T19:49:46.663 に答える
0

私があなたを正しく理解しているならfindViewById(int id)、Activityクラスからあなたが探しているものです。ビューを取得したら、を使用して背景を設定できますsetBackgroundResource(int id)。パラメータIDは、生成されたRファイルにありますfindViewById(R.drawable.row)

于 2010-10-10T19:31:53.313 に答える