Is there such a method call “getBackgroundColor”?

前端 未结 6 1784
旧巷少年郎
旧巷少年郎 2021-01-17 07:29

Is there such a method call \"getBackgroundColor\" in TextView? if I got 2 textViews: tv1 and tv2 in one LinearLayout. What I did:tv1.setBackgroundColor(Color.BLUE)

6条回答
  •  余生分开走
    2021-01-17 08:04

    Here is an additional option:

    The way I solved this problem for my app was to define the colors in values/color.xml.

    
    
        #ff0099cc
        #ff1d1d1d
        #ffffffff
        ...
    
        @color/white
    
    
    

    In the layout the TextView has:

    android:background="@color/textview_background"
    

    If I want to get the background color in code I can just use:

    getResources().getColor(R.color.textview_background)
    

    This gives me a Color object directly without worrying about getting the color from a Drawable.

提交回复
热议问题