标签: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 <ImageView 12 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 <ImageView 21 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 <ImageView 30 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 <ImageView 39 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 <ImageView 48 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 <ImageView 57 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 on 25 // pre-API 17 builds 26 // https://github.com/android/platform_frameworks_base/commit/a7cc06d82e45918c37429a59b14545c6a57db4e4 27 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-handler 40 getActionBar().setDisplayShowTitleEnabled(false); 41 getActionBar().setDisplayShowTitleEnabled(true); 42 43 }
currentColor = newColor;
}
android 在布局中合理的使用tag标签的好处,布布扣,bubuko.com
标签:android des style blog http color 使用 os
原文地址:http://www.cnblogs.com/kingfly13/p/3867911.html