2017-05-12 3 views
1

私は立ち往生しています。レイアウトでは、コードTextViewsから追加します。私は要素のリストを持っており、リストから要素のTextViews依存を追加します。これはうまく動作します。今、私は問題があります。なぜなら、このtextviewsは名前に依存して異なる色の背景を持つ必要があるからです。だから私は、私は、バックグラウンドのためにアンドロイドでさまざまな色のコーデック付きテキストビューを作成するためのアイデア

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" >   
    <stroke 
      android:width="1dp" 
      android:color="@color/common_border_color" /> 

    <solid android:color="#ffffff" /> 

    <padding 
      android:left="1dp" 
      android:right="1dp" 
      android:top="1dp" /> 

    <corners android:radius="5dp" /> 
</shape> 

を使用する場合、私は色を変更することはできません

<resources> 
    <string-array name="colors">   
     <item>#ff0000</item> 
     <item>#00ff00</item> 
     <item>#0000ff</item> 
    </string-array> 
</resources> 

で6色を定義しています。 アイデア

答えて

1

あなたTextViewの背景がshapeある場合は、私は正確に何をしたいかわからない

GradientDrawable gradientDrawable = (GradientDrawable)textView.getBackground(); 
gradientDrawable.setColor(Color.parseColor("#ff0000"));  // change the background color of your TextView to red 
gradientDrawable.setStroke(20, Color.parseColor("#0000ff")); // change the border of your TextView to blue, 20 is the width of the border 
0

ことによってそれの背景や枠線を変更することができます。しかし、あなたは以下のコードを試すことができます。

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 

     <shape xmlns:android="http://schemas.android.com/apk/res/android" 
      android:shape="rectangle"> 
      <gradient 
       android:angle="45" 
       android:centerColor="#ff0000" 
       android:endColor="#00ff00" 
       android:startColor="#0000ff" 
       android:type="linear" /> 
      <corners android:radius="5dp"/> 

     </shape> 
    </item> 
    <item android:top="1dp" android:left="1dp" 
     android:bottom="1dp" android:right="1dp"> 
     <shape android:shape="rectangle"> 
      <corners android:radius="5dp"/> 
      <solid android:color="#aaaaaa" /> 
     </shape> 
     </item> 
</layer-list> 

結果: 私はたとえば5 textviewsのコードからtextviewsを追加したいenter image description here

+0

。すべてのテキストビューは、背景が丸く、この背景の色が異なる必要があります。たとえば、最初のテキストビューには黄色の背景、2番目の赤色の背景などがあります。背景はすべて丸めます – edi233

関連する問題