您的位置:首页 > 移动开发 > Android开发

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

2017-08-08 15:21 127 查看
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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android 界面 布局