Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to understand why a View's background can be null and how that makes sense. Shouldn't there be at least be a default background (ie transparent) that is inherited from the parent view or something?

For example, the following TextView's background attribute is omitted, which results in Background (getBackground()) being null

<TextView
  android:id="@+id/null_bg_tv"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"/>
  <!--android:background="@color/black"-->

TextView tv = v.FindViewById<TextView>(Resource.Id.null_bg_tv);
tv.Background.SetTintList(_csl);  // null exception

Update

A comparison of object dumps show they only differed by a few fields

No background specified (android:background="@null")

Background=
HasOverlappingRendering=False
IsOpaque=False

android:background="@color/black"

Background=android.graphics.drawable.ColorDrawable@5c3246d
HasOverlappingRendering=True
IsOpaque=True

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
379 views
Welcome To Ask or Share your Answers For Others

1 Answer

Applying Styles and Themes to the UI

There are several ways to set a style:

  • To an individual view, by adding the style attribute to a View element in the XML for your layout.
  • To an individual view, by passing the style resource identifier to a View constructor. This is available for apps that target Android 5.0 (API level 21) or higher.
  • Or, to an entire activity or app, by adding the android:theme attribute to the or element in the Android manifest.

When you apply a style to a single View in the layout, the attributes defined by the style are applied only to that View. If a style is applied to a ViewGroup, the child View elements don't inherit the style attributes; only the element to which you directly apply the style applies its attributes. However, you can apply a style so that it applies to all View elements—by applying the style as a theme.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...