我想以编程方式将以下LinearLayout
居中于其父母的中心:
<LinearLayout
android:id="@+id/SignButtonsLinearLayout"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<homes.shared.components.customviews.HomesButton
android:id="@+id/CheckListPDFClosureButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="3dip"
android:drawableLeft="@drawable/CheckListPDFClosure"
android:background="@drawable/MainBlueButton"
style="@style/WhiteDeliveryNoteMenuBold"
android:text="@string/CheckListPDFClosure" />
<homes.shared.components.customviews.HomesButton
android:id="@+id/SignatureAndCloseButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/SignatureAndClosure"
android:background="@drawable/MainBlueButton"
style="@style/WhiteDeliveryNoteMenuBold"
android:text="@string/SignatureAndClose" />
</LinearLayout>
我在这样的代码中实例化了LinearLayout
:
LinearLayout ll = this.FindViewById<LinearLayout>(Resource.Id.SignButtonsLinearLayout);
但该对象没有LayoutGravity
属性
答案 0 :(得分:1)
您可以像这样快速操作:
LinearLayout ll = this.FindViewById<LinearLayout>(Resource.Id.SignButtonsLinearLayout);
ll.SetGravity(GravityFlags.Center);
或者您也可以这样做:
LinearLayout ll = this.FindViewById<LinearLayout>(Resource.Id.SignButtonsLinearLayout);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)lin.LayoutParameters;
layoutParams.Gravity = GravityFlags.Center;
ll.LayoutParameters = layoutParams;
中找到更多信息
编辑:
抱歉,要调整布局重力,以设置View或Layout相对于其父视图的重力,那么您应该能够将此属性添加到axml布局文件中。android:layout_gravity="center"
像这样:
<LinearLayout
android:id="@+id/SignButtonsLinearLayout"
android:layout_gravity="center"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
另一种选择是堆叠布局,因此您可以将线性布局包装在这样的相对布局中,并在父级中使用居中。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/SignButtonsLinearLayout"
android:layout_centerInParent="true"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
</RelativeLayout>