Android 如何将充气布局放置在父布局的中心

Android 如何将充气布局放置在父布局的中心,android,android-layout,android-inflate,layoutparams,Android,Android Layout,Android Inflate,Layoutparams,我正在主布局中展开布局(imageviewlayout)。我正在父视图底部获取子视图。如何将子视图对齐主布局的中心。我已经了解到可以使用Layoutparams来完成此操作。请告诉我如何将子视图对齐主布局的中心 LinearLayout main = (LinearLayout)findViewById(R.id.sendlayout); View childview = getLayoutInflater().inflate(R.layout.imageviewlayout, main,fal

我正在主布局中展开布局(imageviewlayout)。我正在父视图底部获取子视图。如何将子视图对齐主布局的中心。我已经了解到可以使用Layoutparams来完成此操作。请告诉我如何将子视图对齐主布局的中心

LinearLayout main = (LinearLayout)findViewById(R.id.sendlayout);
View childview = getLayoutInflater().inflate(R.layout.imageviewlayout, main,false);          
main.addView(childview);
以下是我的imageviewlayout(子项):


使用
android:layout\u gravity=“center”

见:

将对象放置在其容器的中心(垂直方向和垂直方向) 和水平轴,不改变其大小


PS:由于在子视图中使用容器包装您的
ImageView
,我认为您应该在包装中添加重力属性。这样,“容器”就是您的
main
,而不是子视图的包装器。

我通过在main.xml中添加一个充气布局容器解决了这个问题

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

您是否尝试过android:layout\u centerInParent=“true”?
  <LinearLayout 
    android:id="@+id/inflaterContainerLL" 
    android:layout_height="wrap_content"    
    android:layout_width="match_parent" 
    android:orientation="vertical">   
</LinearLayout>
 LinearLayout main = (LinearLayout)findViewById(R.id.inflaterContainerLL);

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

 main.addView(vchild);