Custom view (custom attribute), custom view attribute
Preface
In the first two articles, we introduced some methods for customizing views, including custom views (inheriting views) and custom views (combining controls ), custom Controls are sometimes lacking in flexible application. Therefore, some custom attributes are required.
Attribute Definition
First, define the required attributes and create an xml file attrs In the res/values/directory. xml. This file defines all the attributes that need to be included. To illustrate this process, it defines an attr_title attribute. As shown below
Attrs. xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 <declare-styleable name="CombinView"> 4 <attr name="attr_title" format="string"></attr>5 </declare-styleable> 6 </resources>
The values available for the format attribute include:
Reference: refer to a resource ID.
Color: color value
Boolean: boolean Value
Dimension: dimension value
Float: floating point value
Integer: integer
String: string
Fraction: Percentage
Enum: enumeration Value
Flag: bit or operation
The preceding content can be used separately or in combination, for example, reference | color, separated by "| ".
Attribute Application
After the attribute is defined, the following describes how to use it, as shown below:
1 <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" 2 xmlns: tools = "http://schemas.android.com/tools" 3 xmlns: cview = "http://schemas.android.com/apk/res/com.example.testcview" 4 android: layout_width = "match_parent" 5 android: layout_height = "match_parent" 6 tools: context = "$ {packageName }. $ {activityClass} "7 8> 9 10 <com. example. testcview. combinView11 android: id = "@ + id/combinView1" 12 android: layout_width = "wrap_content" 13 android: layout_height = "wrap_content" 14 android: layout_alignParentRight = "true" 15 android: layout_alignParentTop = "true" 16 cview: attr_title = "Custom question Header" 17> 18 </com. example. testcview. combinView> 19 20 </RelativeLayout>
Note the following two points,
1> xmlns: cview = "http://schemas.android.com/apk/res/com.example.testcview", You need to declare your namespace, the previous part is
2> set custom properties on the custom control, cview: attr_title = "Custom question Header", you need to add your own name control,
In this way, the custom property is applied to the custom control.
Java code obtains and sets the attribute content
Get the set value in the constructor of the custom control and set it to the corresponding position, as shown below:
1 public class CombinView extends RelativeLayout {2 3 public CombinView (Context context, AttributeSet attrs) {4 super (context, attrs); 5 LayoutInflater inflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE); 6 RelativeLayout layout = (RelativeLayout) inflater. inflate (R. layout. custom_view, this); 7 8 // get custom attributes 9 TypedArray tArray = context. obtainStyledAttributes (attrs, R. styleable. combinView); 10 String title = tArray. getString (R. styleable. combinView_attr_title); 11 12 TextView tvTitle = (TextView) layout. findViewById (R. id. tvTitle); 13 System. out. println ("title =" + title); 14 tvTitle. setText (title); 15 16 tArray. recycle (); 17} 18 19}
Here, you can use the TypedArray tArray = context. obtainStyledAttributes (attrs, R. styleable. CombinView );
String title = tArray. getString (R. styleable. CombinView_attr_title); get the value of the specified attribute
In this way, we will remove the value of the custom attribute. The specific usage depends on the specific requirements.
Postscript
Through this article and the previous two articles, the general process of the custom control is almost the same, but the difference is that on this shelf, the principle is the same.
Address: http://www.cnblogs.com/luoaz/p/3985039.html
An elementary problem is how to associate the activex control made by using the mfc wizard with custom attributes in the attribute dialog box?
See the following two links:
Social.msdn.microsoft.com/..e6cb99
Msdn.microsoft.com/en-us/library/8fb847t6.aspx
To put it simply, Optional property name in VC6 is deleted in 2008, and the control variable must be manually associated with DDP_Text in DoDataExchange.
Void CL18aClockPropPage: DoDataExchange (CDataExchange * pDX)
{
DDP_Text (pDX, IDC_EDIT_INTERVAL, m_updateInterval, _ T ("Interval "));
DDX_Text (pDX, IDC_EDIT_INTERVAL, m_updateInterval );
DDP_PostProcessing (pDX );
}
Note that the DDP_Text (xxxxxxxx) statement should be placed before DDP_PostProcessing (pDX );
Teach you how to make shape with visio-10 (5) Organize your shape with vss
Is the suffix of the visio mold file. (Oh, it's not clear that this stuff is called a mold)
A: drag the shape you have prepared into it. After entering it, you can modify its name. Q: How to save the mold? A: There is a blue disk icon on the right of the modified mold title bar, click. Q: Where is the saved mold? A: If you haven't changed it, it is usually in my documents-my shape folder. The suffix is. vss. One mold corresponds to one. vss. Q: How to open the mold when drawing a: file-shape-my shape. Q: I want to put all the common shapes together.
A: You can drag the shape from the predefined mold to your own mold.
A: double-click the shape in the mold.
A: Sometimes outside the drawing page. File-page settings: page size page, click "Adjust size to fit the drawing content. Q: Do you want to save the edited shape? A: It's useless to save it. The system automatically displays a dialog box asking if you want to update the shape. You can answer this question. 2. How to Set shape attributes Q: What Are shape attributes a: Right-click the shape, and choose shape> Custom Attributes. Q: Do each shape have custom attributes? A: There are some, and some do not ask: how to view the custom attributes of the shape a: two methods. (1) Right-click a shape and choose shape> Custom Attributes (2) View> Custom Attributes. In the dialog box that appears, question: how to add custom attributes to a shape?
A: There are two solutions. (1) Right-click a shape and choose shape> Custom Attributes. Click "define..." in the lower left corner ......"; (2) In the custom Properties window, right-click the menu and choose "define attributes ......"
A: No. It only helps you record the additional information of the Shape for your convenience. For example, if I keep the XS phone number of the water heater in the custom property, but I don't know how many plug-ins it needs during the design, let's look at the custom property and call to ask.
A: Use a custom property set. (1) define the above attributes for one shape first; (2) Right-click the custom Properties window and click the custom property set; (3) in the custom property set window, click "add". In the dialog box that appears, enter a name (for example, "Electrical Product") and select "Create a new property set from the selected shape in visio ", (4) create a new shape. In the custom property set window, select "Electrical Products" and click Apply. OK, your new shape will automatically have all the above attributes. Of course, you still need to manually adjust the attribute value. Review of current highlights:
(1) Build your own mold (2) define your own properties