自定义Layout xml文件转换成View对象和Activity关联的实现
来源:互联网 发布:淘宝快手直播号能买吗? 编辑:程序博客网 时间:2024/05/21 09:20
如何把一个layout文件转换成view对象添加到对应的容器内呢?
下面以一个例子来说明这一点:
主布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="实现Layout和Activity关联"
/>
<Button android:id="@+id/mybutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="测试"/>
</LinearLayout>
把下面一个xml布局文件添加到AlertDialog中:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/relativeLayout1" android:layout_height="fill_parent" android:layout_width="fill_parent" android:padding="10dip">
<TextView android:id="@+id/textView1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="TextView"></TextView>
<EditText android:background="@android:drawable/editbox_background" android:id="@+id/entry" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_below="@+id/textView1" android:layout_alignLeft="@+id/textView1">
<requestFocus></requestFocus>
</EditText>
<Button android:text="确定" android:id="@+id/button1" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:layout_toLeftOf="@id/button1"
android:layout_alignTop="@id/button1"></Button>
<Button android:text="取消" android:id="@+id/button1" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:layout_below="@+id/entry"
android:layout_marginLeft="10dip" android:layout_alignParentRight="true"></Button>
</RelativeLayout>
在Activity如何实现上面布局文件和dialog之间的关联呢 ?
public class Layout_Activity_DialogActivity extends Activity {
private Button mybutton=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mybutton=(Button)findViewById(R.id.mybutton);
mybutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater inflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.view_dialog, null);
new AlertDialog.Builder(Layout_Activity_DialogActivity.this)
.setTitle("My Dialog")
.setMessage("Layout和Dialog关联")
.setView(view)
.show();
}
});
}
}
红色部分的功能就是把一个xml布局文件解析成View对象,然后我们就可以把它添加到任何我们想添加到的容器内。
- 自定义Layout xml文件转换成View对象和Activity关联的实现
- 自定义Layout xml文件转换成View对象和Activity关联的实现
- 用dom4j实现对象和xml文件的互相转换
- 将layout文件转化成View对象的方法
- Android中layout.xml文件中加载自定义的View类
- LayoutInflater作用是将layout的xml布局文件实例化为View类对象。
- LayoutInflater将layout的xml布局文件实例化为View类对象
- 把activity文件转换成view实现画面切换
- 谈谈对象和XML文件的转换
- java自定义注解实现对象到xml配置的转换
- 在Layout xml中配置自定义View
- 在Layout xml中配置自定义View
- android Layout布局转换成View对象的两种方法,如图所示
- 利用XStream实现实体对象和xml文件相互转换
- Unable to start activity ComponentInfo……加载含有自定义View类的xml文件出错
- 自定义view的Layout过程
- 将xml布局转换成view对象
- 【自定义view系列】View的layout过程
- css之clear属性
- 【HDOJ】1196 -> Lowest Bit
- nyoj 108 士兵杀敌(一)
- C++编程学习50个经典网站 强力推荐
- HTML 元素简介
- 自定义Layout xml文件转换成View对象和Activity关联的实现
- Selenium & Nunit GUI & WebDriver
- 实现自定义窗口title
- 驱动函数poll 和 应用里的 select
- 在linux系统添加ip别名
- ios Xcode 安装 cocos2d-x 2.1.4
- VS 2005 处理条件
- Oracle数据库表被锁
- hdu2594(KMP)