动态加载布局和控件

任务背景:使文件管理器Tab栏的控件可以包裹文字内容,不要使文字与边框间距太大,使用Button设置背景后达不到效果,于是考虑使用TextView来实现,重点要解决的问题是    在TextView控件周围捆绑控件(参数网址http://blog.csdn.net/hmily7532361/article/details/16798083)

目录:vendor/mediatek/proprietary/packages/apps/FileManager/src/com/mediatek/filemanager/AbsBaseActivity.java

需要导入的包有:

import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

实现的内容如下:

protected void addTab(String text){
        LinearLayout.LayoutParams mlp=null;
        View viewLikeBtn=null;
        mlp=new LinearLayout.LayoutParams(new ViewGroup.MarginLayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT
        ));//这种方式应该是给控件内部的内容设置宽高,还有一种应该是给控件设置宽高
        TextView txt=new TextView(AbsBaseActivity.this);//动态定义一个TextView对象,传入当前的上下文作为参数
        txt.setTextColor(getResources().getColor(R.color.zuk_gray));//给文本设置颜色
        txt.setText(text);//给控件设置文本内容
        txt.setTextSize(12f);//给文本设置文本字体大小
        txt.setGravity(Gravity.CENTER);//给控件中的内容设置对齐方式
        Drawable rightIcon=getResources().getDrawable(R.drawable.zuk_tab_right);//获得一个Drawable对象
        Drawable leftIcon=getResources().getDrawable(R.drawable.zuk_tab_left);
        rightIcon.setBounds(0,0,rightIcon.getMinimumWidth(),rightIcon.getMinimumHeight());//给Drawable对象设置边界
        leftIcon.setBounds(0,0,leftIcon.getMinimumWidth(),leftIcon.getMinimumHeight());
        txt.setCompoundDrawables(leftIcon,null,rightIcon,null);//给TextView对象设置复合图片,(左,上,右,下)
        txt.setLayoutParams(mlp);//给TextView对象设置布局参数(宽高)
        viewLikeBtn=txt;//把TextView对象赋值给View对象,View对象可以设置监听事件
        viewLikeBtn.setOnClickListener(AbsBaseActivity.this);//在AbsBaseActivity这个类中会继承接口onClickListener,重写方法onClick
        viewLikeBtn.setId(mTabNameList,size());//附带的id信息
        mTabsHolder.addView(viewLikeBtn);
        mTabNameList.add(text);
        viewLikeBtn.setOutlineProvider(null);//这个方法还不清楚
    }

用Button实现的内容如下:

protected void addTab(String text){
        LinearLayout.LayoutParams mlp=null;
        Button button=new Button(AbsBaseActivity.this);
        button.setTextColor(getResources().getColor(R.color.zuk_gray));//给button的文本内容设置颜色
        button.setBackgroundResource(R.drawable.zuk_tab_background);
        //或者button.setBackgroundDrawable(getResources().getDrawable(R.drawable.zuk_tab_background));
        button.setMaxWidth(150);//设置控件的最大宽度
        //button.setMaxHeight(150);//设置控件的最大高度
        button.setText(text);
        button.setTextSize(12f);
        button.setPadding(20,0,20,0);//给控件设置外边距
        //button.setMargins(0,0,0,0);//给控件设置内边距
        button.setGravity(Gravity.CENTER);
        //button.setGravity(Gravity.CENTER_HORIZONTAL);
        //button.setGravity(Gravity.CENTER_VERTICAL);
        button.setEllipsize(TextUtils.TruncateAt.END);//一行显示不了,在尾部缩略为省略号
        //该用法参考网址http://www.cnblogs.com/chiao/archive/2011/08/20/2147250.html
        mlp=new LinearLayout.LayoutParams(new ViewGroup.MarginLayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT
        ));
        //该方法实现点击响应的内容与上面一样
    }

另外一种给LinearLayout设置背景,然后再在布局里面放一个TextView,实习内容如下

	LinearLayout layout=new LinearLayout(AbsBaseActivity.this);//定义一个LinearLayout布局对象
        LinearLayout.LayoutParams mlp=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.MATCH_PARENT);//这个就是设置控件布局参数的方法,上面那个是设置布局内部控件的
        layout.setOrientation(LinearLayout.HORIZONTAL);
        //layout.setOrientation(LinearLayout.VERTICAL);
        layout.setBackgroundResource(R.drawable.zuk_tab_background);

        LinearLayout.LayoutParams textParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        TextView txt=new TextView(AbsBaseActivity.this);
        txt.setTextColor(getResources().getColor(R.color.zuk_gray));
        txt.setText(text);
        txt.setTextSize(10f);
        txt.setLayoutParams(textParams);//给TextView对象设置布局参数
        layout.addView(txt);
        //layout.addView(txt,Params);
        layout.setLayoutParams(mlp);//给线性布局添加布局参数
        viewLikeBtn=txt;
        //该方法实现点击响应的内容与上面一样

动态加载布局和控件参考网址:http://blog.sina.com.cn/s/blog_7256fe8f01016kyb.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值