package com.fxy.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fxy.baselibrary.util.PxAndDp;
import com.fxy.R;

/**
 * 项目名称：FingerShipWork
 * 类描述：带上图下字的组合控件
 * 创建人：ww
 * 创建时间：2018/3/16 13:47
 * 修改人：COMP-021-2002
 * 修改时间：2017/8/28 17:55
 * 修改备注：
 */

public class ImagerViewWithText extends LinearLayout {

    ImageView imageView;
    TextView textView;
    private static final int TEXTSIZE = 20;
    private static final int TEXTVIEWWIDTH = 20;
    int textSize;
    int picture_Id;
    int textViewWidth;
    int defaultWidth;

    public ImagerViewWithText(Context context, AttributeSet attrs) {
        super(context, attrs);
        defaultWidth = PxAndDp.pxToDp(context, TEXTVIEWWIDTH);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ImageViewWithText);
        picture_Id = a.getResourceId(R.styleable.ImageViewWithText_picture, -1);
        textSize = a.getResourceId(R.styleable.ImageViewWithText_textSize, PxAndDp.pxToDp(context, TEXTSIZE));
        textViewWidth = a.getResourceId(R.styleable.ImageViewWithText_textviewwidth, defaultWidth);
        a.recycle();


        initData(context, attrs);

    }

    private void initData(Context context, AttributeSet attrs) {

//        View inflate = LayoutInflater.from(context).inflate(R.layout.item_image_and_text, this,false);

        imageView = /*(ImageView) inflate.findViewById(R.id.ima_item);*/new ImageView(context, attrs);
        imageView.setPadding(10, 10, 10, 10);
        imageView.setImageResource(picture_Id);

        textView = /*(TextView) inflate.findViewById(R.id.tv_item);*/new TextView(context, attrs);
        textView.setPadding(2, 2, 2, 2);
        textView.setTextSize(textSize);
        textView.setWidth(textViewWidth <= defaultWidth ? defaultWidth : textViewWidth);


        setClickable(true);
        setFocusable(true);
        setOrientation(LinearLayout.VERTICAL);
        setGravity(Gravity.CENTER_HORIZONTAL);
        addView(imageView);
        addView(textView);
    }

    public void setText(int resId) {
        textView.setText(resId);
    }

    public void setText(CharSequence buttonText) {
        textView.setText(buttonText);
    }

    public void setTextColor(int color) {
        textView.setTextColor(color);
    }


}
