向界面中加入自定义View的几种方式

0. 将自定义View直接写入Layout并在Activity中引用

MyLayout0.java

public class MyLayout0 extends FrameLayout {
    public MyLayout0(Context context) {
        super(context);
        TextView tv = new TextView(context);
        tv.setBackgroundColor(0xffff0000);
        tv.setText("Layout1");
        addView(tv);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mylayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.demo.DemoActivity" >

    <com.demo.MyLayout0
        android:id="@+id/layout0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

DemoActivity.java

public class DemoActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout myLayout = (LinearLayout) findViewById(R.id.mylayout);
        MyLayout0 layout0 = (MyLayout0) findViewById(R.id.layout0);
    }
}

1. 通过layout文件创建自定义View并在Activity中插入

MyLayout1.java

public class MyLayout1 extends FrameLayout {
    public MyLayout1(Context context) {
        super(context);
    }
}

layout1.xml

<?xml version="1.0" encoding="utf-8"?>
<com.demo.MyLayout1 xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffff00"
        android:text="Layout1" />

</com.demo.MyLayout1>

DemoActivity.java

public class DemoActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout myLayout = (LinearLayout) findViewById(R.id.mylayout);
        MyLayout1 layout1 = (MyLayout1) View.inflate(DemoActivity.this, R.layout.layout1, null);
        myLayout.addView(layout1);
    }
}

2. 在自定义View初始化方法中载入layout并在Activity中插入

MyLayout2.java

public class MyLayout2 extends FrameLayout {
    public MyLayout2(Context context) {
        super(context);
        inflate(context, R.layout.layout2, this);
    }
}

layout2.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#00ff00"
        android:text="Layout2" />

</FrameLayout>

DemoActivity.java

public class DemoActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout myLayout = (LinearLayout) findViewById(R.id.mylayout);
        MyLayout2 layout2 = new MyLayout2(this);
        myLayout.addView(layout2);
    }
}

3. 通过代码创建自定义View并插入Activity

DemoActivity.java

public class DemoActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout myLayout = (LinearLayout) findViewById(R.id.mylayout);
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        FrameLayout layout3 = new FrameLayout(this);
        layout3.setLayoutParams(params);
        TextView tv = new TextView(this);
        tv.setBackgroundColor(0xff0000ff);
        tv.setText("Layout3");
        layout3.addView(tv);
        myLayout.addView(layout3);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伊怡已易

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值