2017-10-13 1 views
0

수신 한 우선 순위에 따라 텍스트 색상을 설정하는 RecyclerView가 있습니다. 우선 순위가 1 (높은 경우) 인 경우 텍스트 색상은 빨간색이고, 그렇지 않으면 텍스트 색상이 기본 색상 (은 android : textColorPrimary 또는 android : textColorSecondary)을 따릅니다. 그러나 attr 파일에서 값을 가져 오는 데 문제가 있습니다. 다음은 attrs.xml 파일의 속성을 사용하여 프로그래밍 방식으로 textColor를 설정하십시오.

내가

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="Theme"> 
     <!-- The most prominent text color. --> 
     <attr name="textColorPrimary" format="reference|color" /> 
     <!-- Secondary text color. --> 
     <attr name="textColorSecondary" format="reference|color" /> 
     <!-- Tertiary text color. --> 
     <attr name="textColorTertiary" format="reference|color" /> 
    </declare-styleable> 
</resources> 

난 단지 알고에서 값을 얻기 위해 노력하고 attrs.xml이의 코드는이 아래

@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    context = parent.getContext(); 
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_layout, 
      parent, false); 

    TextView projectTextView = (TextView) itemView.findViewById(R.id.text_project); 
    TextView timeTextView = (TextView) itemView.findViewById(R.id.text_time); 

    return new ViewHolder(itemView, projectTextView, timeTextView, 
      new ViewHolder.OnItemClickListener() { 
     @Override 
     public void onItemClick(int position) { 
      RecyclerViewAdapter.this.itemClickListener.onItemClick(filteredList.get(position)); 
     } 
    }); 
} 

@Override 
public void onBindViewHolder(ViewHolder holder, int position) { 
    final Message message = filteredList.get(position); //my Message object 

    holder.projectTextView.setText(message.getProject()); 
    holder.timeTextView.setText(message.getTime()); 

    //Get attribute colour from attr file 
    TypedValue typedValue = new TypedValue(); 
    int[] attrs = new int[]{android.R.attr.textColorPrimary, android.R.attr.textColorSecondary}; 
    TypedArray a = context.obtainStyledAttributes(typedValue.data, attrs); 
    int txtColorPrimary = a.getDimensionPixelSize(0, -1); 
    int txtSecondaryPrimary = a.getDimensionPixelSize(1, -1); 
    a.recycle(); 

    //Set the text colour to red if the priority is high ("1") 
    if(message.getPriority().equals("1")){ 
     holder.projectTextView.setTextColor(Color.RED); 
     holder.timeTextView.setTextColor(Color.RED); 
    }else{ 
     //Else set the text colour to default colour 
     holder.projectTextView.setTextColor(txtColorPrimary); //<-- this is not working 
     holder.timeTextView.setTextColor(txtSecondaryPrimary); //<-- this is not working 
    } 


} 

코드 RecyclerViewAdapter 파일에 작업을 얻을 나의 시도 어떻게 레이아웃에서 그것을 설정하지만 자바 코드에서 어떻게 할 지 모르겠다.

안드로이드 : 텍스트 색상 = "안드로이드 : textColorPrimary"

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingStart="16dp" android:paddingLeft="16dp" 
    android:paddingEnd="16dp" android:paddingRight="16dp" 
    android:paddingTop="16dp" 
    android:paddingBottom="16dp"> 


     <TextView 
      android:id="@+id/text_project" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      android:textColor="?android:textColorPrimary" 
      android:maxLines="1" 
      android:ellipsize="end"/> 

     <TextView 
      android:id="@+id/text_time" 
      android:layout_marginLeft="8dp" 
      android:layout_marginStart="8dp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/text_project" 
      android:layout_toEndOf="@id/text_project" 
      android:textStyle="italic" 
      android:textColor="?android:textColorSecondary" 
      android:maxLines="1" 
      android:ellipsize="end" 
      android:gravity="end"/> 


</RelativeLayout> 

내 코드가 잘못된 것 같으면 내가 사과 않습니다. 나는 안드로이드 코드가 정말 좋지 않다는 것을 인정한다. 그리고 이런 식으로하는 것은 어리석은 것처럼 보일지 모르지만 나는 이것을 해결하는데 주어진 조언에 감사한다.

+0

attrs.xml에서 색상을 정의하기위한 특정 요구 사항이 있습니까? –

+0

안녕하세요. 내가 attrs.xml 파일에서 참조 색상을 취하는 것처럼이 방법으로 코드를 관리하는 것이 더 쉽다고 가정합니다. –

+0

스타일을 사용하도록 제안합니다. 메시지의 텍스트 색을 선언하는 2 가지 스타일,'primaryText'와'secondaryText'을 선언합니다. 코드에서 textView의 스타일을 변경할 수 있습니다. [here] (https://developer.android.com/guide/topics/ui/look-and-feel/themes.html) –

답변

0

리소스 (colors.xml)에서 색상을 가져 오지 않는 이유는 무엇입니까?

onBindViewHolder에서 우선 순위 값에 따라 텍스트 색상을 context.getColor(R.color.red)으로 설정할 수 있습니다.

+0

안녕하세요 @Aung Ko Ou, 제 질문을 업데이트했습니다. 다시 문의하십시오. 내가 겪고있는 문제는 실제로 android 속성 인 ** textPrimaryColor ** 및 ** textSecondaryColor **를 가져 와서 내 RecyclerViewAdapter 파일에 설정했습니다. colors.xml에 색상을 추가하지 않았습니다. 안드로이드 attrs.xml에 저장된 기본 텍스트 색상으로하고 싶기 때문에 중복되는 느낌이 들었습니다. –

관련 문제