android 在布局中合理的使用tag标签的好处

<img src="https://img-blog.csdn.net/20150723111211681?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />    有时候相同的按钮页面的切换,在代码中需要进行多个点击的分开的处理,这些其实是不用这样的操作的,在xml布局中使用tag标签可以很好的处理这些问题;简化操作

布局文件如下:

<LinearLayout
 android:id="@+id/colors"
 android:layout_width="match_parent"
 android:layout_height="48dip"
 android:layout_alignParentBottom="true"
 android:layout_marginBottom="8dip"
 android:layout_marginLeft="4dip"
 android:layout_marginRight="4dip"
 android:orientation="horizontal" >

 <ImageView
 android:layout_width="0dip"
 android:layout_height="match_parent"
 android:layout_margin="4dip"
 android:layout_weight="1"
 android:background="#FF666666"
 android:onClick="onColorClicked"
 android:tag="#FF666666" />

 <ImageView
 android:layout_width="0dip"
 android:layout_height="match_parent"
 android:layout_margin="4dip"
 android:layout_weight="1"
 android:background="#FF96AA39"
 android:onClick="onColorClicked"
 android:tag="#FF96AA39" />

 <ImageView
 android:layout_width="0dip"
 android:layout_height="match_parent"
 android:layout_margin="4dip"
 android:layout_weight="1"
 android:background="#FFC74B46"
 android:onClick="onColorClicked"
 android:tag="#FFC74B46" />

 <ImageView
 android:layout_width="0dip"
 android:layout_height="match_parent"
 android:layout_margin="4dip"
 android:layout_weight="1"
 android:background="#FFF4842D"
 android:onClick="onColorClicked"
 android:tag="#FFF4842D" />

 <ImageView
 android:layout_width="0dip"
 android:layout_height="match_parent"
 android:layout_margin="4dip"
 android:layout_weight="1"
 android:background="#FF3F9FE0"
 android:onClick="onColorClicked"
 android:tag="#FF3F9FE0" />

 <ImageView
 android:layout_width="0dip"
 android:layout_height="match_parent"
 android:layout_margin="4dip"
 android:layout_weight="1"
 android:background="#FF5161BC"
 android:onClick="onColorClicked"
 android:tag="#FF5161BC" />
 </LinearLayout>

布局的效果图:

点击不同的颜色的点击切换,更改对应的控件的颜色;

代码的实现如下:

<pre class="java" name="code">public void onColorClicked(View v) {

        int color = Color.parseColor(v.getTag().toString());
        changeColor(color);

    }

 
private void changeColor(int newColor) {

        tabs.setIndicatorColor(newColor);

        // change ActionBar color just if an ActionBar is available
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {

            Drawable colorDrawable = new ColorDrawable(newColor);
            Drawable bottomDrawable = getResources().getDrawable(R.drawable.actionbar_bottom);
            LayerDrawable ld = new LayerDrawable(new Drawable[] { colorDrawable, bottomDrawable });

            if (oldBackground == null) {

                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    ld.setCallback(drawableCallback);
                } else {
                    getActionBar().setBackgroundDrawable(ld);
                }

            } else {

                TransitionDrawable td = new TransitionDrawable(new Drawable[] { oldBackground, ld });

                // workaround for broken ActionBarContainer drawable handling on
                // pre-API 17 builds
                // https://github.com/android/platform_frameworks_base/commit/a7cc06d82e45918c37429a59b14545c6a57db4e4
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    td.setCallback(drawableCallback);
                } else {
                    getActionBar().setBackgroundDrawable(td);
                }

                td.startTransition(200);

            }

            oldBackground = ld;

            // http://stackoverflow.com/questions/11002691/actionbar-setbackgrounddrawable-nulling-background-from-thread-handler
            getActionBar().setDisplayShowTitleEnabled(false);
            getActionBar().setDisplayShowTitleEnabled(true);

        }
           currentColor = newColor;
    }




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Tag使用 package com.yarin.android.qiehuan; import android.app.AlertDialog; import android.app.Dialog; import android.app.TabActivity; import android.content.DialogInterface; import android.graphics.Color; import android.os.Bundle; import android.widget.TabHost; import android.widget.TabHost.OnTabChangeListener; public class Activity01 extends TabActivity { //声明TabHost对象 TabHost mTabHost; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //取得TabHost对象 mTabHost = getTabHost(); /* 为TabHost添加标签 */ //新建一个newTabSpec(newTabSpec) //设置其标签和图标(setIndicator) //设置内容(setContent) mTabHost.addTab(mTabHost.newTabSpec("test1") .setIndicator("TAB 1",getResources().getDrawable(R.drawable.img1)) .setContent(R.id.textview1)); mTabHost.addTab(mTabHost.newTabSpec("test2") .setIndicator("TAB 2",getResources().getDrawable(R.drawable.img2)) .setContent(R.id.textview2)); mTabHost.addTab(mTabHost.newTabSpec("test3") .setIndicator("TAB 3",getResources().getDrawable(R.drawable.img3)) .setContent(R.id.textview3)); //设置TabHost的背景颜色 mTabHost.setBackgroundColor(Color.argb(150, 22, 70, 150)); //设置TabHost的背景图片资源 //mTabHost.setBackgroundResource(R.drawable.bg0); //设置当前显示哪一个标签 mTabHost.setCurrentTab(0); //标签切换事件处理,setOnTabChangedListener mTabHost.setOnTabChangedListener(new OnTabChangeListener() { // TODO Auto-generated method stub @Override public void onTabChanged(String tabId) { Dialog dialog = new AlertDialog.Builder(Activity01.this) .setTitle("善谢谢提醒") .setMessage("现在选了:"+tabId+"标签") .setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { dialog.cancel(); } }).create();//创建按钮 dialog.show(); } }); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值