zoukankan      html  css  js  c++  java
  • Android服务--布局服务(LayoutInflater)

    1. 基本概念

    1. 概念:

    参考资料:https://www.cnblogs.com/androidez/archive/2013/07/01/3164729.html

    一个用于加载布局的系统服务,就是实例化与Layout XML文件对应的View对象,不能直接使用, 需要通过getLayoutInflater( )方法或getSystemService( )方法来获得与当前Context绑定的 LayoutInflater实例;记住:他是一个服务!!

    LayoutInflater这个类它的作用类似于findViewById()。

    不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化;而findViewById()是找xml布局文件下的具体widget控件(如Button、TextView等)。

    具体作用:

    1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;

    2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。

    说白了就是用来动态加载控件的服务

    2. 获得 LayoutInflater 实例的三种方式:

    1.LayoutInflater inflater = getLayoutInflater();  //调用Activity的getLayoutInflater()

    2.LayoutInflater localinflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);   //从这里可以看出来他是一个服务

    3. LayoutInflater inflater = LayoutInflater.from(context);     //很多时候用这一种

    2. 代码

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.aplex_new1.myapplication.MainActivity">
    
    
    
    </android.support.constraint.ConstraintLayout>
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/id_center"
        android:background="@color/colorAccent"
        android:gravity="center"
        tools:context="com.example.aplex_new1.myapplication.MainActivity">
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="inflat 测试"/>
    
    
    </LinearLayout>
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //找到这个界面
            //LinearLayout linerlayout = findViewById(R.id.id_center);
            LayoutInflater inflater = LayoutInflater.from(this);
            //找到test_inflat.xml并且实例化
            LinearLayout ll = (LinearLayout)inflater.inflate(R.layout.test_inflat,null, false);
                    //.findViewById(R.id.id_center);
            //将已经实例化的布局显示出来
            setContentView(ll);
        }
    }

  • 相关阅读:
    iBatis,第二次亲密接触
    微斯人,吾谁与归
    一个月了
    生命在于运动
    眼皮跳了好几天
    往返
    中病毒,学习批处理

    爱如潮水
    今天夏至
  • 原文地址:https://www.cnblogs.com/maogefff/p/7827806.html
Copyright © 2011-2022 走看看