自定义View-1-动态添加view

效果图

//在代码中实现添加布局

这里写图片描述

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:id="@+id/ll_add_context"
    android:background="#424242"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#566667"
        android:gravity="center"
        android:text="FIRST"/>
</LinearLayout>

主页面代码

public class AFragment extends Fragment {

    @Nullable
    @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.fragment_a,null);

        LinearLayout linearLayout=(LinearLayout)view.findViewById(R.id.ll_add_context);
        linearLayout.setOrientation(LinearLayout.VERTICAL);


        TextView textView=new TextView(getActivity());
        textView.setBackgroundColor(getResources().getColor(R.color.colorAccent));
        textView.setText("I am new text");
        linearLayout.addView(textView);//添加view
        LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(200,200);
        linearLayout.getChildAt(1).setLayoutParams(params);//父控件给子view设置布局参数

        TextView button=new TextView(getActivity());
        button.setText("I am the button");
        button.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
        linearLayout.addView(button);//添加view

        RelativeLayout relativeLayout=new RelativeLayout(getActivity());
        LinearLayout.LayoutParams params2=new LinearLayout.LayoutParams(400,400);
        relativeLayout.setLayoutParams(params2);
        relativeLayout.setBackgroundColor(getResources().getColor(R.color.green));

        RelativeLayout.LayoutParams params3=new RelativeLayout.LayoutParams(150,250);
        TextView textView2=new TextView(getActivity());
        textView2.setId(Integer.valueOf(1));//设置viewID
        textView2.setLayoutParams(params3);//view直接设置布局参数
        textView2.setBackgroundColor(getResources().getColor(R.color.colorAccent));
        textView2.setText("I am new text 2");
        relativeLayout.addView(textView2);

        RelativeLayout.LayoutParams params4=new RelativeLayout.LayoutParams(150,150);
        params4.addRule(RelativeLayout.BELOW,textView2.getId());//父控件为RelativeLayout时,设置布局属性的规则
        TextView button2=new TextView(getActivity());
        button2.setText("I am the button 2");
        button2.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
        relativeLayout.addView(button2);
        relativeLayout.getChildAt(1).setLayoutParams(params4);

        linearLayout.addView(relativeLayout);
        return view;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值