当前位置:首页 » 安卓系统 » 安卓控件在哪里

安卓控件在哪里

发布时间: 2023-11-30 22:13:40

㈠ android怎么获取 中的控件

在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater是用来找res/layout/下的xml布局文件代码块,并且实例化;而findViewById()是找xml布局文件下的具体widget控件(如Button、TextView等)。 具体作用:
1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;
2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。
LayoutInflater 是一个抽象类,在文档中如下声明:
public abstract class LayoutInflater extends Object
获得 LayoutInflater 实例的三种方式:
1.LayoutInflater inflater = getLayoutInflater(); //调用Activity的getLayoutInflater()
2.LayoutInflater localinflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
1. LayoutInflater inflater = LayoutInflater.from(context);
其实,这三种方式本质是相同的,从源码中可以看出:
getLayoutInflater():
Activity 的 getLayoutInflater() 方法是调用 PhoneWindow 的getLayoutInflater()方法,看一下该源代码:
public PhoneWindow(Context context) {
super(context);
mLayoutInflater = LayoutInflater.from(context);
}
可以看出它其实是调用 LayoutInflater.from(context)。
LayoutInflater.from(context):
public static LayoutInflater from(Context context) {
LayoutInflater LayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (LayoutInflater ==null) {
throw new AssertionError("LayoutInflater not found.");
}
return LayoutInflater;
}
可以看出它其实调用 context.getSystemService()。
结论:所以这三种方式最终本质是誉基胡都是调庆拦用的Context.getSystemService()。锋伍
inflate 方法 通过 sdk 的 api 文档,可以知道该方法有以下几种过载形式,返回值均是 View 对象,如下:
public View inflate (int resource, ViewGroup root);
3 public View inflate (XmlPullParser parser, ViewGroup root);
4 public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot);
5 public View inflate (int resource, ViewGroup root, boolean attachToRoot);
6
7 LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
8 View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test));
9 //EditText editText = (EditText)findViewById(R.id.content);
10 // error
EditText editText = (EditText)view.findViewById(R.id.content);
对于上面代码,指定了第二个参数 ViewGroup root,当然你也可以设置为 null 值。
注意:
·inflate方法与 findViewById 方法不同;
·inflater 是用来找 res/layout下的 xml 布局文件,并且实例化;
·findViewById() 是找具体 xml 布局文件中的具体 widget 控件(如:Button、TextView 等)。

㈡ android中获得控件的位置(相对于布局文件)

正确的代码如下所示

java">TextViewtv=(TextView)findViewById(R.id.text);
tv.getViewTreeObserver().addOnGlobalLayoutListener(newOnGlobalLayoutListener(){
@Override
publicvoidonGlobalLayout(){
//这里执行获取位置大小操作
intleft=tv.getLeft();
inttop=tv.getTop();
intbottom=tv.getBottom();
intright=tv.getRight();
//tv相对于父布局的坐标就得出来了

}
}

view的位置和大小是通过onLayout和onMeasure方法计算出来的,执行到activity的onCreate()方法时,尚未开始计算控件的大小和位置,所以是取不到的

OnGlobalLayoutListener 是ViewTreeObserver的内部类,当一个视图树的布局发生改变时,可以被ViewTreeObserver监听到,这是一个注册监听视图树的观察者(observer),在视图树的全局事件改变时得到通知

除了OnGlobalLayoutListener ,ViewTreeObserver还有如下内部类:


interfaceViewTreeObserver.OnGlobalFocusChangeListener

当在一个视图树中的焦点状态发生改变时,所要调用的回调函数的接口类


interfaceViewTreeObserver.OnGlobalLayoutListener

当在一个视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变时,所要调用的回调函数的接口类


interfaceViewTreeObserver.OnPreDrawListener

当一个视图树将要绘制时,所要调用的回调函数的接口类


interfaceViewTreeObserver.OnScrollChangedListener

当一个视图树中的一些组件发生滚动时,所要调用的回调函数的接口类


interfaceViewTreeObserver.OnTouchModeChangeListener

当一个视图树的触摸模式发生改变时,所要调用的回调函数的接口类

㈢ android 一个layout中有很多个子控件,怎么去获得其中的一个控件

一个layout就是一个容器,你可以放一些子控件,当你要操作子控件的时候,你就需要找对应的子控件,再去操作它(操作比如设置字体,颜色,样式等)。
可以通过findViewById()方法 来获取,前提是,你的子控件需要定义一个id,然后在通过该方法来寻找并操作它。

还可以通过findViewWithTag()方法来找到子控件(前提是你第一次找到这个子控件时,并设置一个tag),该方法一般用在activity调用adapter里面填充布局里面的子控件。

热点内容
ios应用上传 发布:2024-09-08 09:39:41 浏览:439
ios储存密码哪里看 发布:2024-09-08 09:30:02 浏览:871
opensslcmake编译 发布:2024-09-08 09:08:48 浏览:653
linux下ntp服务器搭建 发布:2024-09-08 08:26:46 浏览:744
db2新建数据库 发布:2024-09-08 08:10:19 浏览:173
频率计源码 发布:2024-09-08 07:40:26 浏览:780
奥迪a6哪个配置带后排加热 发布:2024-09-08 07:06:32 浏览:101
linux修改apache端口 发布:2024-09-08 07:05:49 浏览:209
有多少个不同的密码子 发布:2024-09-08 07:00:46 浏览:566
linux搭建mysql服务器配置 发布:2024-09-08 06:50:02 浏览:995