在Activity中我们需要实例化很多layout控件的对象,但是对象分为很多情况,以下就为主要的几种情况:
第一种:实例化在Activity中setContextView(R.layout...)的布局中的button,textview等控件
private ListView listView
listView= (ListView) findViewById(R.id.list_view_home);
第二种:实例化在Activity中setContextView(R.layout...)的布局中的其他子布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/qgreen"> <!--空间大小用px,局利用sp,dp--> <!--加载标题样式文件--> <include android:id="@+id/layout_title_bar" layout="@layout/title_bar"/> <include android:id="@+id/layout_progress" layout="@layout/progress" /> <ListView android:id="@+id/list_view_home" android:layout_height="wrap_content" android:layout_width="match_parent" android:dividerHeight="1dp" android:cacheColorHint="#000000" android:paddingBottom="80dp" /><!--android:divider="@drawable/line"--> </LinearLayout>对于上面的<include>中的值布局,实例化代码如下:应该统一用View来实例化对象,后面与上面一样:
private View progressView; private View titleView;
progressView=findViewById(R.id.layout_progress); //R.id.layout_progress为home不居中的progress控件的id titleView=findViewById(R.id.layout_title_bar); //这个为标题样式的对象
第三种:实例化没有在Activity中setContextView(R.layout...)的其他布局
private View MoreMsgView; //这个对象用于加载more布局
MoreMsgView=View.inflate(this,R.layout.the_more_layout,null); //inflate()的作用就是将一个用xml定义的布局文件查找出来,注意与findViewById()的区别,inflate是加载一个布局文件,而findViewById则是从布局文件中查找一个控件。
也是用view来实例化对象
第四种:实例化没有在Activity中setContextView(R.layout...)的其他布局,中的控件
private View MoreMsgView; //这个对象用于加载more布局
MoreMsgView=View.inflate(this,R.layout.the_more_layout,null); //inflate()的作用就是将一个用xml定义的布局文件查找出来,注意与findViewById()的区别,inflate是加载一个布局文件,而findViewById则是从布局文件中查找一个控件。
上面为加载其他布局,下面为该布局中的控件对象的实例化:
private LinearLayout linearLayoutMore;
linearLayoutMore= (LinearLayout) MoreMsgView.findViewById(R.id.llyt_loading);多了一个MoreMsgView.这个对象
第五种:在不是Activity的类中,实例化一个该类没有加载的布局
首先:
private LayoutInflater mlayoutInflater;
mlayoutInflater=LayoutInflater.from(context); //在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化;而findViewById()是找xml布局文件下的具体widget控件(如Button、TextView等)。 具体作用: // 1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入; // 2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。
然后:Linearlayout为布局的类型,为线性布局
private LinearLayout mlinearLayout;
mlinearLayout= (LinearLayout) mlayoutInflater.inflate(R.layout.refresh_listview,this,false);
之后:
public ImageView mimageView; public TextView mtextView;
mimageView= (ImageView) mlinearLayout.findViewById(R.id.img_View); mtextView= (TextView) mlinearLayout.findViewById(R.id.refresh_textView);
第六种:不是Activity的类在其中,加载布局,实例化控件
LayoutInflater layoutInflater = LayoutInflater.from(getContext()); View view = layoutInflater.inflate(R.layout.tool_bar, null); mTextTitle = (TextView) view.findViewById(R.id.toolbar_title); mSearchView = (EditText) view.findViewById(R.id.toolbar_searchview); mRightButton = (Button) view.findViewById(R.id.toolbar_rightButton);
以上就是所有对象的实例化的总结