Domanda

I am inflating a layout(imageviewlayout) in Main layout.I am getting the child view at t he bottom of parent view.How can I align the child view at the center of Mainlayout.I have read that it can be done using Layoutparams .can you please tell how to align the child view at the center of main layout.

LinearLayout main = (LinearLayout)findViewById(R.id.sendlayout);
View childview = getLayoutInflater().inflate(R.layout.imageviewlayout, main,false);          
main.addView(childview);

here is my imageviewlayout(child):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"
    android:orientation="vertical" >
     <ImageView
        android:id="@+id/thumbnailImage"
        android:layout_width="250px"
        android:layout_height="250px"
        android:layout_gravity="center_horizontal"
        android:scaleType="fitCenter"
        android:src="@drawable/camera" />

</LinearLayout>
È stato utile?

Soluzione

Use android:layout_gravity="center".

See LayoutParams:

Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.

PS: Since you wrap your ImageView with a container in your child view, I believe you should add gravity property in the wrapper. That way the "container" is your main, not the child view's wrapper.

Altri suggerimenti

I solved this by addding a inflaterlayout container in main.xml

  <LinearLayout 
    android:id="@+id/inflaterContainerLL" 
    android:layout_height="wrap_content"    
    android:layout_width="match_parent" 
    android:orientation="vertical">   
</LinearLayout>

and refernced the inflaterContainerLL in the main.xml activity

 LinearLayout main = (LinearLayout)findViewById(R.id.inflaterContainerLL);

 View vchild = getLayoutInflater().inflate(R.layout.imageViewlayout, main,false);

 main.addView(vchild);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top