在代码中生成View

快一个月没有写文章了,学习之路不能懈怠,风雨中前行。

最近学习了关于用代码来实现布局的设计,记录一下主要的实现。

一、写成一个类,去继承自一种View,然后这个新的View类就可以直接使用了

public class CommonTitleView extends FrameLayout {
   public CommonTitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater mInflater = LayoutInflater.from(context);
        mView = mInflater.inflate(R.layout.title_view_container_view, null);
        mContext = context;
        addView(mView);
        setupView = findViewById(R.id.title_bar_left_view);
        titleTV = (TextView) findViewById(R.id.title_bar_title_tv); //默认标题栏初始化
        titleTV.setVisibility(VISIBLE);
        backIBtn = (ImageButton) findViewById(R.id.title_bar_back_ibtn); //左边的返回按钮默认初始化
        backIBtn.setVisibility(VISIBLE);
        rightSideTV = (TextView) findViewById(R.id.title_bar_right_tv0); //右边的按钮默认初始化
        rightSideTV.setVisibility(GONE);
        //标题栏设置默认颜色和字体大小
        titleTV.setTextSize(mTitleHeadTextSize);
        titleTV.setTextColor(mTitleBarTextColor);
        setDefaultTextColorAndSize();
    }
}

可以直接在xml中使用该View

<com.gta.goldencock.android.library.commontitle.CommonTitleView
        android:id="@+id/title_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/main_tab_textColor_select">

    </com.gta.goldencock.android.library.commontitle.CommonTitleView>



二、在代码中生成并返回一个View

    private View init(View userView) {
        if (isViewNull()) {
            return;
        }
        //在代码中生成一个View
        containerLayout = new FrameLayout (context);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams (ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        containerLayout.setLayoutParams (params);
        containerLayout.setBackgroundResource (R.color.white);

        FrameLayout.LayoutParams params1 = new FrameLayout.LayoutParams (FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT);
        params1.gravity = Gravity.CENTER;
        containerLayout.addView(userView,params1);
        return containerLayout;
    }

三、代码中混合使用View

 
    public void setContentView(@LayoutRes int layoutResID) {
        //依附于整个最外层布局
        View view = LayoutInflater.from(this).inflate(R.layout.acitivity_base_with_title, null);
        //用户布局
        View addView = LayoutInflater.from(context).inflate(layoutResID , null)
        View userView = init(addView);
        //生成最外层布局的内容容器
        LinearLayout contentLayout = (LinearLayout) view.findViewById(R.id.frame_content);
        contentLayout.removeAllViews();
        contentLayout.addView(userView);
        //返回最后把userView放在最上层的View
        super.setContentView(view);
    }



以上就是简单的View的组合使用,需要注意的是依附的View一定不能有父类布局,不然会抛出异常。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值