Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

android - TextView background null when not set in layout

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...