Basic writing method of view custom composite control in Android

In many cases, as long as we make good use of Android to provide me with good controls, they are cleverly combined through layout, which is a new control, which I call "custom composite control".

So, when is this custom composite control used? Or when you are doing a project, you will find that some layouts will be reused, and the XML code blocks of the same layout will be copied and pasted repeatedly, which will cause confusion in the code structure and increase the amount of code. All kinds of controls need to be specified and processed in Java code, and the workload is really not small. Therefore, we must find a reasonable method Let's take a look at a diagram to implement a simple user-defined composite control based on a simple layout.

From the above figure, we can see that there are no "brand-new" controls in this layout, which are all native controls of Android system. People familiar with the Android interface layout must think that this layout is really a small case. It is too simple and can be written in minutes. So here is the layout code of an item:

<!--?xml version=1.0 encoding=utf-8?-->
<relativelayout android:background="@drawable/selector_blue" android:id="@+id/rl_show_address" android:layout_height="60dip" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android">
 
  <textview android:id="@+id/tv_title" android:layout_height="wrap_content" android:layout_marginleft="5dip" android:layout_margintop="1dip" android:layout_width="wrap_content" android:text="这是标题" android:textcolor="#000000" android:textsize="20sp">
 
  <textview android:id="@+id/tv_desc" android:layout_below="@id/tv_title" android:layout_height="wrap_content" android:layout_marginleft="6dip" android:layout_margintop="1dip" android:layout_width="wrap_content" android:text="这是描述内容" android:textcolor="#99ff0000" android:textsize="14sp">
 
  <check@R_353_2419@ android:clickable="false" android:focusable="false" android:id="@+id/cb_status" android:layout_alignparentright="true" android:layout_centervertical="true" android:layout_height="wrap_content" android:layout_width="wrap_content">
   
  <!-- 加一条分割线 -->
  <view android:background="#000000/" android:layout_alignbottom="@id/cb_status" android:layout_alignparentbottom="true" android:layout_height="0.2dip" android:layout_margintop="7dip" android:layout_width="match_parent">
 
</view></check@R_353_2419@></textview></textview></relativelayout>

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>