[英]Android relative layout center
在我的相對布局中,我嘗試將2個數字選擇器居中,第一個數字選擇器居中,但是第二個數字選擇器僅在第一個數字選擇器后添加,它們不均等地居中。 我認為它缺少一些小東西,但無法正常工作。 在我的代碼下方,並用紅色顯示了該問題的圖片。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<NumberPicker
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true" />
<NumberPicker
android:id="@+id/test2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_toRightOf="@+id/test" />
<EditText
android:id="@+id/test3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/purchaseCent"
android:layout_centerHorizontal="true"
android:background="@drawable/edittext_bottom_bg"
android:hint="@string/pin"
android:inputType="numberPassword"
android:padding="10dp"
android:textColorHint="#cccccc" />
<ImageButton
android:id="@+id/test4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/test2"
android:background="@drawable/round_button"
android:onClick="testMethod"
android:src="@drawable/ic_test" />
</RelativeLayout>
嘗試添加一個“空格”。
它將是1dp寬(以便完全可交易)並且完全透明。
它必須具有一個ID,以便NumberPickers可以引用它。
將此放在中間。
其他兩個視圖將被簡單地分配在其左側的一個視圖和位於其右側的一個視圖。
解決了。
嘗試將兩個NumberPickers
“存儲”到同一對象(即另一個RelativeLayout
以便可以並排放置它們),然后根據需要居中。
嘗試這種方式,希望這將幫助您解決問題。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<NumberPicker
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<NumberPicker
android:id="@+id/test2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
</LinearLayout>
<EditText
android:id="@+id/test3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edittext_bottom_bg"
android:hint="@string/pin"
android:inputType="numberPassword"
android:padding="10dp"
android:layout_marginTop="10dp"
android:textColorHint="#cccccc" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="bottom|center_horizontal">
<ImageButton
android:id="@+id/test4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/round_button"
android:onClick="testMethod"
android:src="@drawable/ic_test" />
</LinearLayout>
</LinearLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.