UI布局设计之加载xml

一般在activity中通过setContentView()将界面显示出来,但是如果在非activity中如何对控件布局设置操作,这需LayoutInflater动态加载
1. Activity中调用layout
1.setContentView(R.layout.main)
一旦调用, layout就会立刻显示UI
2.调用xml
mWidgetCanvas = (WidgetCellLayout) getFragmentManager().findFragmentById(
R.id.canvas_content);
其中public final class WidgetCellLayout extends Fragment
Main.xml中定义
<fragment
android:id="@+id/canvas_content"
android:layout_width="@dimen/widget_canvas_width"
android:layout_height="@dimen/widget_canvas_height"
android:layout_alignParentTop="true"
android:layout_gravity="center_horizontal"
class="com.google.tv.launcher.ui.WidgetCellLayout" />
2. 非activity中操作layout
final LayoutInflater inflater = LayoutInflater.from( mContext);
LinearLayout layout = (LinearLayout) inflater.inflate(
R.layout.listview, null ).findViewById(R.id.layout);
inflate只会把Layout形成一个以view类实现成的对象,有需要时再用setContentView(view)显示出来
LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件

ListView lv = (ListView)layout.getChildAt( 0 );
或者
LayoutInflater mInflater = LayoutInflater.from(context);
View myView = mInflater.inflate(R.layout.customToast, null);
TextView sender = (TextView) myView.findViewById(R.id.sender);
TextView message = (TextView) myView.findViewById(R.id.message);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值