2013-03-13 37 views
14

私はXMLで定義されたTextViewを持っていて、それに背景色と境界線を設定したいと思います。 問題私は、XMLで私はすでにandroid:backgroundボーダーリソースを設定しているので、私はもう一度背景色のために使用することはできません。 誰かが私を正しい方向に導くことができますか?textViewのボーダーと背景色を設定します

Border.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > 
    <solid android:color="#ffffff" /> 
    <stroke android:width="1dip" android:color="#7F000000"/> 
</shape> 

のTextView

<TextView 
     android:id="@+id/editor_title" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:background="@drawable/title_border"   
     android:padding="5dp" 
     android:text="@string/editor_title"    
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

答えて

29

次に、あなたの単一の背景として設定することができ、このためのXML描画可能を作成する必要があります。ここにあなたが望むものがあります(異なる色の境界線を持つ長方形 - それを望まない場合はグラデーションを置き換えてください)。 XMLを介し

TextView c1 = new TextView(activity); 
c1.setTextColor(getResources().getColor(R.color.solid_red)); 
c1.setText("My Text");  

TextView test = (TextView) view.findViewById(R.id.textView2); 
test.setBackgroundResource(R.color.holo_green_light); 

<?xml version="1.0" encoding="utf-8"?> 
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <stroke android:width="3dp" android:color="@color/blue_button_border" /> 
    <gradient 
     android:startColor="@color/gradient_end" 
     android:endColor="@color/gradient_start" 
     android:angle="-90" /> 
</shape> 
3

経由のJava:これはあなたの '描画可能' フォルダに移動します

<TextView 
     android:text="2" 
     android:textSize="200sp" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/textView2" 
     android:style="@style/textviewStyle" 
     android:background="@android:color/holo_green_light" 
     android:gravity="center" 
     android:textColor="#EEEEEE" 
     android:layout_alignParentRight="true" /> 

これは、このトピックに関するAPIのページです。 http://developer.android.com/guide/topics/resources/accessing-resources.html#ResourcesFromXml

関連する問題