note 21-cuntom component

from:

 

 http://www.cnblogs.com/tt_mc/archive/2010/05/28/1746307.html

 http://blog.csdn.net/notice520/article/details/6667827

 

 

 

custom component :

 

1. make an custom layout xml file as a  template .

 

2. Inflate the xml file to get the layout instence .

 

3. Must use the cuntom_view.findViewById(int id) to find the views inside it.

 

4.  two types of the inflate function;

 

 

   a.  inflate the cunstom layout xml to the empty view I instances . attachToRoot must be true;

//method 1 match R.layout.custom_dialog to view;
        LinearLayout view=new LinearLayout(context);
        inflater.inflate(R.layout.custom_dialog,view,true);

 

   b. return the view that get by inflate function

 //method 2 , return the view that has been match;
      View view=inflater.inflate(R.layout.custom_dialog, null);

 

 

custom_dialog.xml:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip"
    >
<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_margin="10dip"
    />
<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:textColor="#ffffff"
    />
</LinearLayout>

 

 

 

package com.layoutInflater_test;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class LayoutInflaterTest01 extends Activity
{
    
    private Button inflateButton;
    
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        inflateButton=(Button)this.findViewById(R.id.inflateButton);
        inflateButton.setOnClickListener(inflateListener);
      
    }
    
    private OnClickListener inflateListener=new OnClickListener(){

        public void onClick(View arg0) {
//            throw new UnsupportedOperationException("Not supported yet.");
            showCustomDialog();
        }
        
    };
    
    
    private void showCustomDialog(){
        AlertDialog.Builder builder;
        AlertDialog alertDialog;
        
        Context context=LayoutInflaterTest01.this;
        
        LayoutInflater inflater=
                (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        
        //method 1 match R.layout.custom_dialog to view;
        LinearLayout view=new LinearLayout(context);
        inflater.inflate(R.layout.custom_dialog,view,true);
        
        //method 2 , return the view that has been match;
//        View view=inflater.inflate(R.layout.custom_dialog, null);
        
        LinearLayout mainLayout=(LinearLayout)inflater.inflate(R.layout.main,null);
        
        
        // id only can be found by the directly root view
        ImageView img=(ImageView)view.findViewById(R.id.image);
//        ImageView img=(ImageView)LayoutInflaterTest01.this.findViewById(R.id.image);   // fail
//        ImageView img=(ImageView)mainLayout.findViewById(R.id.image);                  // fail
        img.setImageResource(R.drawable.pdf_icon);
        
        TextView text=(TextView)view.findViewById(R.id.text);
//        ImageView img=(ImageView)LayoutInflaterTest01.this.findViewById(R.id.text);    //fail
//        TextView text=(TextView)mainLayout.findViewById(R.id.text);                    //fail
        text.setText("inflater testing");
        
        builder=new AlertDialog.Builder(context);
        builder.setView(view);
        alertDialog=builder.create();
        alertDialog.show();
        
//        LayoutInflaterTest01.this.setContentView(view);
        
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值