custom views

0

I need to dynamically add to my main activity (a number) a view that I have defined in an xml file in the layouts folder. I have defined the custom view as a RelativeLayout where I have different types of views. The problem I have is that after inflating the custom view and gregar the main activity the elements of the custom view are disordered and all superimposed in the center of my mainactivity
 this is the xml file of my custom view:

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

    <TextView
        android:text="TextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginStart="11dp"
        android:layout_marginTop="27dp"
        android:id="@+id/textView2"
        />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="47dp"
        android:id="@+id/button" />

    <TextView
        android:text="TextView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView1"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/textView2" />

    <TextView
        android:text="TextView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView3"
        android:layout_alignBottom="@+id/button"
        android:layout_alignStart="@+id/textView2" />
</RelativeLayout>

in this way I add the view to the main activity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    vistaPadre=(LinearLayout)findViewById(R.id.mainLinear);
    LayoutInflater inflar= LayoutInflater.from(this);
    int id = R.layout.vistapersonalizada;
vistaPadre = (LinearLayout) findViewById(R.id.mainLinear);
layoutPlantilla = (RelativeLayout) inflar.inflate(id,null,false);
vistaPadre.addView(layoutPlantilla);

Here is a screenshot of how the main activity view is added

    
asked by E.B 18.03.2017 в 14:02
source

1 answer

1

Good mate EB, testing the code I can generate the same result as samples, this happens because your main XML is a linearlayout and your custom is a relative layout in which its elements use the position of these for its reordering, the inconvenience you can solve it by changing the layout type in your main xml, as an example I leave you as I did.

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.datamex.jsanchez.relative.MainActivity">

LinearLayout>

Step to be:

RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.datamex.jsanchez.relative.MainActivity">

RelativeLayout>

And this must also be changed to match the type of object:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    vistaPadre=(LinearLayout)findViewById(R.id.mainLinear);
    LayoutInflater inflar= LayoutInflater.from(this);
    int id = R.layout.vistapersonalizada;
vistaPadre = (LinearLayout) findViewById(R.id.mainLinear);
layoutPlantilla = (RelativeLayout) inflar.inflate(id,null,false);
vistaPadre.addView(layoutPlantilla);

to this:

    vistaPadre=(RelativeLayout)findViewById(R.id.activity_main);
    LayoutInflater inflar= LayoutInflater.from(this);
    int id = R.layout.mainlinear;
    vistaPadre = (RelativeLayout) findViewById(R.id.activity_main);
    layoutPlantilla = (RelativeLayout) inflar.inflate(id,null,false);
    vistaPadre.addView(layoutPlantilla);

I hope you get the answer, please report any errors to see if we can help you. Greetings.

    
answered by 18.03.2017 в 15:10