自定义View

<?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="match_parent"
    android:orientation="horizontal">

    <ImageView
        android:layout_marginTop="10dp"
        android:id="@+id/title_img1"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:src="@drawable/default_sdk_login"/>
    <ImageView
        android:layout_marginTop="10dp"
        android:id="@+id/title_img2"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:src="@drawable/detail_loading"/>
    <ImageView
        android:layout_marginTop="10dp"
        android:id="@+id/title_img3"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:src="@drawable/detail_search_icon"/>
</LinearLayout>

public class TitleView extends LinearLayout{
    ImageView img1;
    ImageView img2;
    ImageView img3;
    public TitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public void setLeftListener(OnClickListener listener){
        img1.setOnClickListener(listener);
    }
    public void setRightListener(OnClickListener listener){
        img3.setOnClickListener(listener);
    }
    public TitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public TitleView(Context context) {
        super(context);
    }
    private void initView(Context context){
        LayoutInflater.from(context).inflate(R.layout.title_main,this);
        img1 = (ImageView) findViewById(R.id.title_img1);
        img2 = (ImageView) findViewById(R.id.title_img2);
        img3 = (ImageView) findViewById(R.id.title_img3);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="test.bwei.com.xiangmu.activity.MainActivity">

    <test.bwei.com.xiangmu.view.TitleView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/myTitle"></test.bwei.com.xiangmu.view.TitleView>
</RelativeLayout>


public class MainActivity extends AppCompatActivity {
    TitleView titleView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setTitle();
    }
    private void initView(){
        titleView = (TitleView) findViewById(R.id.myTitle);
    }
    private void setTitle(){
        titleView.setLeftListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"哎哎哎",Toast.LENGTH_LONG).show();
            }
        });
        titleView.setRightListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"aaa",Toast.LENGTH_LONG).show();
            }
        });
    }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值