Use getBackground().setAlpha, which causes the background transparency of other layouts to change

A project I'm working on recently includes a title bar that changes its transparency as the ScrollView slides. What is used is getBackground().setAlpha to achieve, it has no problem in versions below Android 5.0, but in systems above 5.0, it will cause other layouts that share a resource (for example: @color/white) The transparency is the same as the title. The column was changed.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/ll_title_bar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/white"
        android:orientation="vertical" />

    <LinearLayout
        android:id="@+id/ll_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:orientation="vertical" />

</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

For example, after using getBackground().setAlpha to change the transparency of ll_title_bar, the transparency of ll_content will also be changed.

I found information on the Internet to know that when multiple controls in the layout use a resource at the same time, these controls will share a state, such as ColorState. If you change the state of a control, other controls will receive the same notification . At this time, we can use the mutate() method to make the control state indeterminate, so that the indeterminate state control will not share its own state.

titleLayout.getBackground().mutate().setAlpha(255);
Original address: http://blog.csdn.net/myatlantis/article/details/49336587






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325903556&siteId=291194637