How to make such an element? Created a class inherited from View . How to bind it to a markup file and add it dynamically. A vivid example of the wall VKontakte, namely the posts. For this you need to use the fragment ?
1 answer
After creating a custom component, in the markup you need to specify the full path to the class of this component. You can post it in any controller - and activations and fragments.
For example, you created a custom View whose class name is MyView with the following package:
package com.example.mycustomview public class MyView extends View{ ... } In any markup you can place it as follows:
<com.example.mycustomview.MyView android:layout_width="match_parent" android:layout_height="match_parent" > </com.example.mycustomview.MyView> In order to add dynamically, you just need to create an instance of this class and attach it to the View to which you want to add it, just as with the default widgets of the system.
In the activation \ fragment there should be a markup (here - main.xml) which contains a container for placing your dynamic View inside itself (here the root LinearLayout with id - @ + id / mainlayout):
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LinearLayout mainLayout = (LinearLayout)findViewById(R.id.mainlayout); MyView myView = new MyView(MainActivity.this); LayoutParams myViewLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); myView.setLayoutParams(myViewLayoutParams); mainLayout.addView(myView); } - Once again I find myself thinking that before asking a question, you need to write in English in a search engine. Found an article, translate and add later. It describes how to make an element whose class inherits from
Layout. In my case, I usedRelativeLayout. - Herrgott