码迷,mamicode.com
首页 > 移动开发 > 详细

android自定义View之(七)------自定义控件组合仿actionbar控件

时间:2015-02-03 21:25:16      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:android   custom   view   actionbar   

   我们前面写了6个自定义view的样例,这都是全新自已画的控件。在这个样例中,我们来用几个现有的控件来组合成一个新的控件。

   效果图:

   我们用二个Button和一个TextView组合来成为一个actionbar,下面先来一个效果图:

技术分享

关键代码:

(1)res/layout/custom_action_bar.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="wrap_content" >
    
    <Button
        android:id="@+id/button_ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_ok_click"
		android:layout_alignParentLeft="true"/>
    
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:visibility="gone"
        />

    <Button
        android:id="@+id/button_no"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_no_click"
        android:layout_alignParentRight="true"/>

</RelativeLayout>

(2)CustomActionbar.java------自定义组合控件

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class CustomActionbar extends RelativeLayout{
	
	private Button button_ok;
	private Button button_no;
	private TextView title;
	
	private ActionbarClickedListener actionbarClickedListener;

	public CustomActionbar(Context context) {
        this(context, null);
    }
 
    public CustomActionbar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
 
    public CustomActionbar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        LayoutInflater.from(context).inflate(R.layout.custom_action_bar, this, true);
    }
		
    protected void onFinishInflate() {
        super.onFinishInflate();
        button_ok = (Button) findViewById(R.id.button_ok);
        button_ok.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				// TODO Auto-generated method stub
				  if (actionbarClickedListener != null) {
					  actionbarClickedListener.onActionbarOKClicked();
	                }
			}
		});
        button_no = (Button) findViewById(R.id.button_no);
        button_no.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				// TODO Auto-generated method stub
				  if (actionbarClickedListener != null) {
					  actionbarClickedListener.onActionbarCancelClicked();
	                }
			}
		});
        
        title = (TextView) findViewById(R.id.title);
    }
	
    public void setTitle(int resId){
    	title.setText(resId);
    	title.setVisibility(View.VISIBLE);
    }
 
    public void setActionbarClickedListener(ActionbarClickedListener actionbarClickedListener){
    	this.actionbarClickedListener = actionbarClickedListener;
    }
    
    public interface ActionbarClickedListener {
        public void onActionbarOKClicked();
        public void onActionbarCancelClicked();
    }
}

(3)res/layout/activity_main.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="wrap_content">

<com.example.customviewactionbar.CustomActionbar
android:id="@+id/customActionbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>


(4)MainActivity.java-----主文件

import com.example.customviewactionbar.CustomActionbar.ActionbarClickedListener;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;

public class MainActivity extends Activity implements ActionbarClickedListener{
	
	public static final String TAG ="CustomActionbar";
	
	private CustomActionbar customActionbar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
	}

	private void init() {
		// TODO Auto-generated method stub
		customActionbar = (CustomActionbar) findViewById(R.id.customActionbar);
		customActionbar.setActionbarClickedListener(this);
		//customActionbar.setTitle(getResources().getText(R.string.hello_world));
		customActionbar.setTitle(R.string.hello_world);
	}

	@Override
	public void onActionbarOKClicked() {
		// TODO Auto-generated method stub
		Log.i(TAG,"click_ok");
	}

	@Override
	public void onActionbarCancelClicked() {
		// TODO Auto-generated method stub
		Log.i(TAG,"click_cancel");
		finish();
	}
}

组合控件自定义view总结:

1.自定义组合控件得到想要的控件

2.自定义控件,导入布局文件,实现自已想要控件

3.在其它布局文件中使用


源码下载:

http://download.csdn.net/detail/hfreeman2008/8421039


参考资料:

1.Android软件开发之 自定义控件

http://blog.csdn.net/jackhenry/article/details/7340474

2.android - 自定义(组合)控件 + 自定义控件外观

http://www.cnblogs.com/bill-joy/archive/2012/04/26/2471831.html

android自定义View之(七)------自定义控件组合仿actionbar控件

标签:android   custom   view   actionbar   

原文地址:http://blog.csdn.net/hfreeman2008/article/details/43412863

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!