2021-10-12 implementation of common reusable components, such as navigation bar

Keywords: Android html

Learning Lib_ Knowledge in common business module

Declare styleable: custom control properties

Sometimes the official controls are not enough. We need to define our own controls,

① Define attrs.xml file under res/values file to define the controls we need;

*<?xml version="1.0" encoding="utf-8"?>* 
*<resources>* 
  *<declare-styleable name="ToolBar">* 
    *<attr name="buttonNum" format="integer"/>* 
    *<attr name="itemBackground" format="reference|color"/>* 
  *</declare-styleable>* 
*</resources>*

You need to surround all attributes with.

Where name is the name of the attribute set, which is mainly used to identify the attribute set. Where will it be used? Mainly in the third step. See? "R.styleable.ToolBar_buttonNum" is used when obtaining an attribute ID. obviously, it adds "ToolBar_" in front of each attribute.

Then comes format, which is the format of this attribute set. Let's look at the types of various attributes: string, integer, dimension, reference, color, enum... If the attribute can pass two different attributes at the same time, it can be separated by "|".

② Use the properties defined above in the layout file when using

*<?xml version="1.0" encoding="utf-8"?>* 
*<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"* 
  *xmlns:toolbar="http://schemas.android.com/apk/res/cn.zzm.toolbar"* 
  *android:orientation="vertical"* 
  *android:layout_width="fill_parent"* 
  *android:layout_height="fill_parent"* 
  *>* 
  *<cn.zzm.toolbar.ToolBar android:id="@+id/gridview_toolbar"* 
    *android:layout_width="fill_parent"* 
    *android:layout_height="wrap_content"* 
    *android:layout_alignParentBottom="true"* 
    *android:background="@drawable/control_bar"* 
    *android:gravity="center"* 
    *toolbar:buttonNum="5"* 
    *toolbar:itemBackground="@drawable/control_bar_item_bg"/>* 
*</RelativeLayout>*

③ Introduced when used in kt

TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.ToolBar); 
buttonNum = a.getInt(R.styleable.ToolBar_buttonNum, 5); 
itemBg = a.getResourceId(R.styleable.ToolBar_itemBackground, -1);
a.recycle() //Release resources

Get the hidden pain of the attribute set in the custom component. Here is the fixed format. First, get the TollBar and name it a, and then get the attribute set in the styleable through a.getxx. xx is the format of this property set,

reference: Specifies the resource ID in the Theme.

1. Definition:

   <declare-styleable name="My">
        <attr name="label" format="reference" >
    </declare-styleable>

2. Use:

<Buttonzkx:label="@string/label" >

Title bar common to MyToolBar

The returned header is used in many activities, so we set it as public.

First, there is an icon similar to an arrow in the layout. Using imageview, click the image to return. Then there is the corresponding title of the activity, so there is a textview, and then some other controls. If many controls need to be added, if some are not used, set their visibility to invisible on the inapplicable page.

① We want to customize a view. The [toolBar] can be reused. Create a view to inherit LinearLayout. Here, create a MyToolBarLayout to inherit,

② Write an xml for the view, [mytitleview] in the layout file, only these controls and IDs are defined, but no specific resource source is specified, that is, the picture is not directly inserted, and the value of textview is not directly specified.

③ Under values, the attr custom control [MyView] defines public_bar and declare the property sets of title, res, img and right,

④ Load the components in the layout file in the defined view, obtain the property set of the custom control, and set resources for the layout control.

//Initialize custom components
private void initview(Context context, AttributeSet attrs) {
    //Load layout file
    View view = LayoutInflater.from(context).inflate(R.layout.mytitleview, null);
    //Initialize controls for layout
    textView_left=view.findViewById(R.id.mytitleview_left_text);
    textView_right=view.findViewById(R.id.mytitleview_right_text);
    textView_center=view.findViewById(R.id.mytitleview_center_text);
    
    
    //get attribute
    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyView);
    //Resource (parameter: 1. Resource id 2. Default value)
    int left = typedArray.getResourceId(R.styleable.MyView_myleft, 0);
    int right = typedArray.getResourceId(R.styleable.MyView_myright, 0);
    int center = typedArray.getResourceId(R.styleable.MyView_mycenter, 0);
    //To judge whether it is empty, the default value will be taken, and if it is not empty, the resource will be set
    if(left!=0){
        textView_left.setText(left);
    }
    if(right!=0){
        textView_right.setText(right);
    }
    if(center!=0){
        textView_center.setText(center);
    }
    //add to
    this.addView(view);
    //Release resources
    typedArray.recycle();
}

⑤ The view is used in other places

<view The full pathname of the
	andoird: id = 
	android:layout_width = 
	android:layout_height =
	app: Myview_myleft (MyView_mycenter etc.) = 
/>

It can be used to assign values to resources. Here is MyView_myleft.

Posted by Killswitch on Tue, 12 Oct 2021 17:32:26 -0700