Combination control of custom control

A lot of custom controls are used in development, most of which are used in combination with several controls. Here is the link to the combined controls. I am not too familiar with custom controls. I read the information yesterday and wrote a small one myself. demo, let’s start with the simple, combine the controls, and share the complex ones with everyone. I won’t say much, let’s go to the code.
Let ’s take an example first. For example, we need a control, a text on the left, and a text on the right. The following A horizontal line,


the demo address is attached below
https://github.com/wudongze/WidgetDemo
to upload the code
first to realize the layout of that piece

<?xml version="1.0" encoding="utf-8"?>
< RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="45dp">


    <TextView
        android:id="@+id/tv_left"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:padding="5dp" />

    <TextView
        android:id="@+id/tv_right"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:gravity="center"
        android:padding="5dp" />

    <TextView
        android:id="@+id/line"
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>

然后定义自己需要的属性在attrs.xml文件中

<declare-styleable name="MyTextLayout">
    <attr name="left_text" format="string"/><!--左文本-->
    <attr name="right_text" format="string"/><!--right text-->
    <attr name="line_color" format="color"/><!--line color-->
    <attr name ="left_text_size" format="dimension"/><!--The size of the left text-->
    <attr name="right_text_size" format="dimension"/><!--The size of the right text-->
    <attr name ="left_text_color" format="color"/><!--The color of the left text-->
    <attr name="right_text_color" format="color"/><!--The color of the right text-->
    <attr name ="is_show_line" format="boolean"/><!--Whether to show horizontal line-->
</declare-styleable>

Here name is the name of the attribute, format is the type of the attribute, and the specific type will be prompted, choose according to your own business area.
Let start writing the code below.

public class MyTextLayout extends RelativeLayout {
    private TextView tvLeft, tvRight;
    private TextView line;
    private RelativeLayout relativeLayout;
    //This constructor is used for setting in code, that is, dynamically using controls
    public MyTextLayout(Context context) {
        super(context);
    }
   //This constructor is used to set properties of controls in the layout file
    public MyTextLayout (Context context, AttributeSet attrs) {
        super(context, attrs);
        //Get view
        inflate(getContext(), R.layout.text_layout, this);
        tvLeft = (TextView) findViewById(R.id.tv_left);
        tvRight = (TextView) findViewById(R.id.tv_right);
        line = (TextView) findViewById(R.id.line);
        relativeLayout = (RelativeLayout) findViewById(R.id.root);
       //Get TypedArray, which has our definition various properties, as follows
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attrs, R.styleable.MyTextLayout);
        String leftText = obtainStyledAttributes.getString(R.styleable.MyTextLayout_left_text);
        String rightText = obtainStyledAttributes.getString(R.styleable.MyTextLayout_right_text);
        int lineColor = obtainStyledAttributes.getColor(R.styleable.MyTextLayout_line_color, Color.parseColor("#ffffff"));
        float leftTextSize = obtainStyledAttributes.getDimension(R.styleable.MyTextLayout_left_text_size,15.0f);
        float rightTextSize = obtainStyledAttributes.getDimension(R.styleable.MyTextLayout_right_text_size,15.0f);
        int leftTextColor = obtainStyledAttributes.getColor(R.styleable.MyTextLayout_left_text_color,Color.parseColor("#000000"));
        int rightTextColor = obtainStyledAttributes.getColor(R.styleable.MyTextLayout_right_text_color,Color.parseColor("#000000"));
        boolean isShowLine = obtainStyledAttributes.getBoolean(R.styleable.MyTextLayout_is_show_line,true);
       //为view中的控件赋值
        tvLeft.setText(leftText);
        tvRight.setText(rightText);

        tvLeft.setTextSize(leftTextSize);
        tvRight.setTextSize(rightTextSize);

        tvLeft.setTextColor(leftTextColor);
        tvRight.setTextColor(rightTextColor);

        line.setBackgroundColor(lineColor);

        if (isShowLine) line.setVisibility(isShowLine ? VISIBLE : GONE);
    }
   //This constructor is used to set the default style control
    public MyTextLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

   //The following are some methods defined by yourself, according to the business 1. Set the text on the left 2. Get the text content on the left 3. Set the text on the right
   //4. Get the text on the right 5. Set the display and hide of the horizontal line 6 、Set click listener
    public void setLeftText(String leftText) {
        tvLeft.setText(leftText);
    }

    public String getLeftText() {
        return tvLeft.getText().toString();
    }

    public void setRightText(String rightText) {
        tvRight.setText( rightText);
    }

    public String getRightText() {
        return tvRight.getText().toString();
    }

    public void isShowLine(boolean flag) {
        line.setVisibility(flag ? VISIBLE : GONE);
    }

    public void setOnClickLintener(OnClickListener onClickLintener) {
        relativeLayout.setOnClickListener(onClickLintener);
    }
}

在使用的时候和正常的控件一样

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.wudz.widgetdemo.MyTextLayout
        android:id="@+id/my_text"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:is_show_line="false"
        app:left_text="product price"
        app:line_color="#000000"
        app:right_text="20 yuan"></com.example.wudz.widgetdemo.MyTextLayout>

</RelativeLayout>

Here you can use the attributes we define Now , be sure to note that when using custom controls, be sure to add

xmlns:app="http://schemas.android.com/apk/res-auto"

This is the
namespace Set

myTextLayout = (MyTextLayout) findViewById in the code (R.id.my_text);
myTextLayout.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(MainActivity.this,"点击了控件",Toast.LENGTH_SHORT).show();
        myTextLayout.setLeftText("商品价格2");
        myTextLayout.setRightText("40元");
        myTextLayout.isShowLine(true);
    }
});

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326526855&siteId=291194637