Android layout_centerInParent 属性 以编程方式添加视图后不起作用
Android layout_centerInParent property not working after adding a view programatically
我有一个相对布局,其中包含一个 linearLayout
和两个 imageView
,如下所示。
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rlParent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:layout_centerInParent="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_marginRight="5dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"/>
</LinearLayout>
</RelativeLayout>
然后我以编程方式添加了自定义 surfaceView
。添加 surfaceView
linearLayout
后未显示在 center.I 中尝试通过代码设置 layout_centerInParent
属性 但它不起作用。
请帮助我。
mPreview = new CustomSurfaceView(getActivity(),1, CameraPreview.LayoutMode.FitToParent, false,this);
LayoutParams previewLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
mLayout.addView(mPreview, 0, previewLayoutParams);
mLayout为Relativelayout,mPreview为自定义surfaceview
试试这个 android:layout_centerHorizontal="true"
而不是 android:layout_centerInParent="true"
您的父布局(此处RelativeLayout
)宽度必须为match_parent
。那么只有你的子布局,centerInParent
会起作用。
试试这个...
<RelativeLayout
android:id="@+id/rlParent">
<LinearLayout
android:id="@+id/rlChild">
......
</LinearLayout>
View mPreview = findViewById(R.id.rlChild);
RelativeLayout.LayoutParams previewLayoutParams = (RelativeLayout.LayoutParams)mPreview.getLayoutParams();
previewLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
mPreview.setLayoutParams(previewLayoutParams);
mLayout.addView(mPreview);
第二行和第三行,你要写match_parent
。这就是 centerinparent
不起作用的原因。
我有一个相对布局,其中包含一个 linearLayout
和两个 imageView
,如下所示。
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rlParent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:layout_centerInParent="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_marginRight="5dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"/>
</LinearLayout>
</RelativeLayout>
然后我以编程方式添加了自定义 surfaceView
。添加 surfaceView
linearLayout
后未显示在 center.I 中尝试通过代码设置 layout_centerInParent
属性 但它不起作用。
请帮助我。
mPreview = new CustomSurfaceView(getActivity(),1, CameraPreview.LayoutMode.FitToParent, false,this);
LayoutParams previewLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
mLayout.addView(mPreview, 0, previewLayoutParams);
mLayout为Relativelayout,mPreview为自定义surfaceview
试试这个 android:layout_centerHorizontal="true"
而不是 android:layout_centerInParent="true"
您的父布局(此处RelativeLayout
)宽度必须为match_parent
。那么只有你的子布局,centerInParent
会起作用。
试试这个...
<RelativeLayout
android:id="@+id/rlParent">
<LinearLayout
android:id="@+id/rlChild">
......
</LinearLayout>
View mPreview = findViewById(R.id.rlChild);
RelativeLayout.LayoutParams previewLayoutParams = (RelativeLayout.LayoutParams)mPreview.getLayoutParams();
previewLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
mPreview.setLayoutParams(previewLayoutParams);
mLayout.addView(mPreview);
第二行和第三行,你要写match_parent
。这就是 centerinparent
不起作用的原因。