Adding a view to a LinearLayout at a specified position

后端 未结 6 890
[愿得一人]
[愿得一人] 2020-12-25 09:52

I have the following main.xml file with a LinearLayout




        
相关标签:
6条回答
  • 2020-12-25 10:06

    The docs state you can use the index to insert it where you want. I see you are using the signature of the view only, did you try the signature with the index parameter?

    public void addView(View child, int index) 
    
    0 讨论(0)
  • 2020-12-25 10:12
    setContentView(R.layout.main);
    
    ImageView img = (ImageView) findViewById(R.id.imagefield);
    img.setImageResource(your_image_here);
    

    and in the xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:weightSum="1"
        android:id="@+id/llid">
    
        <TextView android:text="Client profile"
            android:id="@+id/ProfileName"
            android:layout_width="fill_parent"
            android:textStyle="bold"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal">
        </TextView>    
    
        <ImageView android:id="@+id/imagefield" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content">
        </ImageView> 
    
        <TextView android:text="Specs"
            android:id="@+id/Specs"
            android:layout_width="fill_parent"
            android:textStyle="bold"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal">
        </TextView>
    
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-25 10:15

    Add an ImageView into the xml, and if its not being used, make it invisible (image.setVisibility(View.INVISIBLE)). It may not show anything anyway when no image is set.

    0 讨论(0)
  • 2020-12-25 10:23

    To get position of view in a view group

                    val targetPosition = oldLL.indexOfChild(viewToAdd)
    

    To add a view at a position in a view group

                    newLL.addView(viewToAdd,targetPosition)
    
    0 讨论(0)
  • 2020-12-25 10:27

    I faced a similar problem. In my case I wanted to add a LinearLayout at last position of another LinearLayout. To accomplish it, I did:

    LinearLayout parentLayout = (LinearLayout) findViewById(R.id.parentLayout);
    LinearLayout layout = new LinearLayout(this);
    layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    
    // add others views to your linear layout
    
    parentLayout.addView(layout, parentLayout.getChildCount());
    
    0 讨论(0)
  • 2020-12-25 10:30

    When adding a View to a ViewGroup, you can specify an index which sets the position of the view in the parent.

    You have two views and so (counting from zero) you would want to add at the 1st position; just call ll.addView(image, 1); to have it placed in between the two TextViews.

    0 讨论(0)
提交回复
热议问题