Android之自定义组合view

来源:互联网 发布:fc2最新手机域名官方 编辑:程序博客网 时间:2024/05/01 22:34

本文主要记录一些零碎的东西

项目中发现要重复写的东西太多了,开始自己造轮子,这个自定义的view很简单,只需要几步就好了,

这里的自定义的view,只要是实现类似于actionBar的顶部菜单栏功能,使用组合控件,即使用系统提供的组件,而不是自己去绘制,把所需组件组合在一起,我的如下  

<?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="40dp"    >    <LinearLayout        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="1">    </LinearLayout>    <LinearLayout        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="10"        android:gravity="center">        <TextView            android:id="@+id/home_top_business"            android:layout_width="50dp"            android:layout_height="25dp"            android:gravity="center"            android:textColor="#000"            android:background="@drawable/corners_bg"            android:text="@string/business"/>        <TextView            android:id="@+id/home_top_wares"            android:layout_width="50dp"            android:layout_height="25dp"            android:gravity="center"            android:textColor="#000"            android:background="@drawable/corners_bg"            android:text="@string/wares"/>        <TextView            android:id="@+id/home_top_favorites"            android:layout_width="50dp"            android:layout_height="25dp"            android:gravity="center"            android:textColor="#000"            android:background="@drawable/corners_bg"            android:text="@string/favorites"/>    </LinearLayout>    <LinearLayout        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="1"        >        <ImageView            android:id="@+id/share"            android:layout_width="20dp"            android:layout_height="match_parent"            android:src="@drawable/share"            />    </LinearLayout></LinearLayout>
然后新建一个自定义的HomeTopview,继承view或者其子类都可以

View 派生出来的直接或间接子类:ImageView, Button, CheckBox, SurfaceView, TextView, ViewGroup, AbsListView
ViewGourp 派生出来的直接或间接子类:AbsoluteLayout, FrameLayout, RelativeLayout, LinearLayout

我的代码:

import android.content.Context;import android.content.Intent;import android.graphics.Canvas;import android.util.AttributeSet;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.widget.LinearLayout;import android.widget.TextView;import com.cl.slack.tse.R;import com.cl.slack.tse.activity.HomeActivity;import com.cl.slack.tse.fragment.HomeFragment;/** * Created by chenling on 2016/4/14. * 自定义view,组合控件 ,解放top 菜单 */public class HomeTopView extends LinearLayout {    private TextView home_top_business,home_top_wares,home_top_favouries;//顶部三个TextView    private Context mContext;    private HomeTopListener homeTopListener;    //一般我们这样使用时会被调用,View view = new View(context);    public HomeTopView(Context context) {        super(context);    }    //当我们在xml布局文件中使用View时,会在inflate布局时被调用,    // <View layout_width="match_parent" layout_height="match_parent"/>。    public HomeTopView(Context context, AttributeSet attrs) {        super(context, attrs);        Log.i("slack","HomeTopView...super(context, attrs)......");        mContext = context;        LayoutInflater.from(mContext).inflate(R.layout.home_top, this);        home_top_business = (TextView)findViewById(R.id.home_top_business);        home_top_business.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Log.i("slack", "onClick.........homeBusinessFragment");                if(homeTopListener != null){                    homeTopListener.businessClick();                }else {                    changecolor();                    home_top_business.setBackgroundResource(R.drawable.corners_select_bg);                    ((HomeActivity)mContext).getSupportFragmentManager().beginTransaction().replace(R.id.home_change, HomeFragment.homeBusinessFragment).commit();                }            }        });        home_top_wares = (TextView) findViewById(R.id.home_top_wares);        home_top_wares.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Log.i("slack", "onClick.........homeWaresFragment");                if(homeTopListener != null){                    homeTopListener.waresClick();                }else {                    changecolor();                    home_top_wares.setBackgroundResource(R.drawable.corners_select_bg);                    ((HomeActivity) mContext).getSupportFragmentManager().beginTransaction().replace(R.id.home_change, HomeFragment.homeWaresFragment).commit();                }            }        });        home_top_favouries = (TextView) findViewById(R.id.home_top_favorites);        home_top_favouries.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Log.i("slack", "onClick.........homeFavouriesFragment");                if(homeTopListener != null){                    homeTopListener.favouriesClick();                }else {                    changecolor();                    home_top_favouries.setBackgroundResource(R.drawable.corners_select_bg);                    ((HomeActivity) mContext).getSupportFragmentManager().beginTransaction().replace(R.id.home_change, HomeFragment.homeFavouriesFragment).commit();                }            }        });        //分享        findViewById(R.id.share).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if(homeTopListener != null){                    homeTopListener.shareClick();                }else {                    Intent shareIntent = new Intent();                    shareIntent.setAction(Intent.ACTION_SEND);                    shareIntent.putExtra(Intent.EXTRA_TEXT, "我的博客地址:http://blog.csdn.net/i_do_can");                    shareIntent.setType("text/plain");                    //设置分享列表                    mContext.startActivity(Intent.createChooser(shareIntent, "分享到"));                }            }        });    }    private void changecolor() {        home_top_business.setBackgroundResource(R.drawable.corners_bg);        home_top_wares.setBackgroundResource(R.drawable.corners_bg);        home_top_favouries.setBackgroundResource(R.drawable.corners_bg);    }    /*    * 跟第二种类似,但是增加style属性设置,这时inflater布局时会调用第三个构造方法。    * <View style="@styles/MyCustomStyle" layout_width="match_parent" layout_height="match_parent"/>。    * */    public HomeTopView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    //set 方法    public void setHomeTopListener(HomeTopListener homeTopListener) {        this.homeTopListener = homeTopListener;    }    //回调事件,可以不implement 但是只要实现了,必须写方法    public interface HomeTopListener{        void businessClick();        void waresClick();        void favouriesClick();        void shareClick();    }}

有三个构造函数需要我们实现,我这里使用的是组合控件,只需要在HomeTopView(Context context, AttributeSet attrs)里就行

LayoutInflater.from(mContext).inflate(R.layout.home_top, this);这句话需要注意,如果是在fragment里,这里的第二个参数我习惯性的写 null,一开始我也是这么写的,后来发现直接报错,查看了郭霖大神的文章,才发现问题

这里我写了一个接口,方便在不同界面上更改顶部菜单栏里控件的点击事件,这个接口怎么使用,类似如下代码

public class Fragment extends Fragment implements HomeTopView.HomeTopListener{    private HomeTopView homeTopView;        @Nullable    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        view = inflater.inflate(R.layout.home_base_fragment,null);        homeTopView = (HomeTopView)view.findViewById(R.id.home_top);        homeTopView.setHomeTopListener(this);        return view;    }    @Override    public void businessClick() {        Log.i("slack","businessClick...");    }    @Override    public void waresClick() {        Log.i("slack","waresClick...");    }    @Override    public void favouriesClick() {        Log.i("slack","favouriesClick...");    }    @Override    public void shareClick() {        Log.i("slack","shareClick...");    }}
这样就可以简单实现自定义的组合view了。





0 0