向界面中加入自定义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
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
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
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自定义view的几种方式
- 记录自定义View的几种方式
- Android自定义View实现的几种方式
- Android界面设计的4种方式之四——开发自定义的View
- Android TextView加下划线的几种方式
- 自定义view-仿虎扑直播比赛界面的打赏按钮
- 使用XIB自定义一个UIView,然后将这个view添加到controller的view 上(相当于所有界面都通过xib来实现)
- Android实战简易教程-第三十二枪(自定义View登录注册界面EditText-实现一键清空)
- 【Android UI】Android开发之View的几种布局方式及实践
- Android开发:view的几种布局方式
- RecyclerView 数据刷新的几种方式
- 自定义View的界面和行为
- 自定义View界面大合集
- Android TextView 添加下划线的几种方式
- 安卓使用WebView 自定义错误界面
- 自定义View实例(二)----一步一步教你实现QQ健康界面
- Android自定义View-登录界面
- android里TextView加下划线的几种方式
- 使用setContentView的方式更换布局文件从而更换界面
- js调用刷新界面的几种方式