android 在布局中合理的使用tag標籤的好處

來源:互聯網
上載者:User

標籤:android   des   style   blog   http   color   使用   os   

有時候相同的按鈕頁面的切換,在代碼中需要進行多個點擊的分開的處理,這些其實是不用這樣的操作的,在xml布局中使用tag標籤可以很好的處理這些問題;簡化操作

 布局檔案如下:

 1 <LinearLayout 2         android:id="@+id/colors" 3         android:layout_width="match_parent" 4         android:layout_height="48dip" 5         android:layout_alignParentBottom="true" 6         android:layout_marginBottom="8dip" 7         android:layout_marginLeft="4dip" 8         android:layout_marginRight="4dip" 9         android:orientation="horizontal" >10 11         <ImageView12             android:layout_width="0dip"13             android:layout_height="match_parent"14             android:layout_margin="4dip"15             android:layout_weight="1"16             android:background="#FF666666"17             android:onClick="onColorClicked"18             android:tag="#FF666666" />19 20         <ImageView21             android:layout_width="0dip"22             android:layout_height="match_parent"23             android:layout_margin="4dip"24             android:layout_weight="1"25             android:background="#FF96AA39"26             android:onClick="onColorClicked"27             android:tag="#FF96AA39" />28 29         <ImageView30             android:layout_width="0dip"31             android:layout_height="match_parent"32             android:layout_margin="4dip"33             android:layout_weight="1"34             android:background="#FFC74B46"35             android:onClick="onColorClicked"36             android:tag="#FFC74B46" />37 38         <ImageView39             android:layout_width="0dip"40             android:layout_height="match_parent"41             android:layout_margin="4dip"42             android:layout_weight="1"43             android:background="#FFF4842D"44             android:onClick="onColorClicked"45             android:tag="#FFF4842D" />46 47         <ImageView48             android:layout_width="0dip"49             android:layout_height="match_parent"50             android:layout_margin="4dip"51             android:layout_weight="1"52             android:background="#FF3F9FE0"53             android:onClick="onColorClicked"54             android:tag="#FF3F9FE0" />55 56         <ImageView57             android:layout_width="0dip"58             android:layout_height="match_parent"59             android:layout_margin="4dip"60             android:layout_weight="1"61             android:background="#FF5161BC"62             android:onClick="onColorClicked"63             android:tag="#FF5161BC" />64     </LinearLayout>

布局的:

點擊不同的顏色的點擊切換,更改對應的控制項的顏色;

代碼的實現如下:

1 public void onColorClicked(View v) {2 3         int color = Color.parseColor(v.getTag().toString());4         changeColor(color);5 6     }

 

 

 1 private void changeColor(int newColor) { 2  3         tabs.setIndicatorColor(newColor); 4  5         // change ActionBar color just if an ActionBar is available 6         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 7  8             Drawable colorDrawable = new ColorDrawable(newColor); 9             Drawable bottomDrawable = getResources().getDrawable(R.drawable.actionbar_bottom);10             LayerDrawable ld = new LayerDrawable(new Drawable[] { colorDrawable, bottomDrawable });11 12             if (oldBackground == null) {13 14                 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {15                     ld.setCallback(drawableCallback);16                 } else {17                     getActionBar().setBackgroundDrawable(ld);18                 }19 20             } else {21 22                 TransitionDrawable td = new TransitionDrawable(new Drawable[] { oldBackground, ld });23 24                 // workaround for broken ActionBarContainer drawable handling on25                 // pre-API 17 builds26                 // https://github.com/android/platform_frameworks_base/commit/a7cc06d82e45918c37429a59b14545c6a57db4e427                 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {28                     td.setCallback(drawableCallback);29                 } else {30                     getActionBar().setBackgroundDrawable(td);31                 }32 33                 td.startTransition(200);34 35             }36 37             oldBackground = ld;38 39             // http://stackoverflow.com/questions/11002691/actionbar-setbackgrounddrawable-nulling-background-from-thread-handler40             getActionBar().setDisplayShowTitleEnabled(false);41             getActionBar().setDisplayShowTitleEnabled(true);42 43         }

           currentColor = newColor;

    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.