LayoutInflater

LayoutInflater是在一个layout里面嵌套另一个layout。

最长用的实例是所有界面顶部具有返回按钮的标题栏。如图所示。


首先创建被嵌套的小layout

xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg"
    android:orientation="horizontal" >
    
    <Button 
        android:id="@+id/btnback"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_margin="7px"
        android:gravity="left"
        android:background="@drawable/cancel"
        />
    
    <TextView 
        android:id="@+id/txttitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_gravity="center"
        android:textSize="50px"
        android:layout_weight="20"
        android:text="title"
        />
    
    <Button 
        android:id="@+id/btnclose"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:layout_margin="7px"
        android:layout_weight="1"
        android:background="@drawable/close"
        />


</LinearLayout>


其次是制作这个xml相对应的java文件:

package com.base.title;


import com.example.test02.R;


import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;


public class TitleLayout extends LinearLayout{


public TitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.titleone, this);

Button btnClose = (Button)findViewById(R.id.btnclose);
btnClose.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
((Activity)getContext()).finish();
}
});
}


}


最后就是使用这个layout的文件。

使用文件也包含两个一个是xml另一个是java文件

先来看下xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
<com.base.title.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
></com.base.title.TitleLayout>


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />


</RelativeLayout>


注意:package的路径一定要写

再看java文件:

package com.base.title;


import com.example.test02.R;


import android.os.Bundle;
import android.app.Activity;
import android.view.Window;


public class TitleTwoActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_title_two);
}


}


运行一下,点X按钮就关掉了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值