您好,欢迎来到九壹网。
搜索
您的当前位置:首页在代码中生成View

在代码中生成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一定不能有父类布局,不然会抛出异常。


因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- 91gzw.com 版权所有 湘ICP备2023023988号-2

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务