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 색을 정의했습니다. 아이디어가 있으십니까? 당신의 TextView의 배경이 shape 경우

답변

1

, 당신은 당신이 원하는 정확히 모르는

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

. 모든 텍스트 뷰는 둥근 배경과이 배경의 다른 색상을 가져야합니다. 예 : 첫 번째 텍스트 뷰에는 노란색 배경, 두 번째 빨간색 배경 등이 있습니다. 모든 배경은 둥글게됩니다. – edi233