[英]RelativeLayout align Textview and Edittext
我想使用RelativeLayout將文本視圖水平對齊,然后再編輯文本,然后再次進行textview然后是edittext。 我知道您可以使用LinearLayout做到這一點,但我想通過relativeLayout做到這一點。 我的代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/userNameLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username:"
android:layout_alignParentLeft="true" />
<EditText
android:id="@+id/userNameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/userNameLbl"
android:minWidth="200dp"
android:text="Sample Text" />
<TextView
android:id="@+id/pwdLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password:"
android:layout_toRightOf="@+id/userNameText"
android:layout_toEndOf="@+id/userNameText" />
<EditText
android:id="@+id/userNameText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/pwdLbl"
android:minWidth="200dp"
android:text="Sample Text" />
</RelativeLayout>
但這是行不通的。 所有控件( edittexts和textviews )都放在彼此之間! 我的代碼有什么問題? 我已經使用layout_toRightOf
將它們彼此相鄰。 並且因為提到了,所以控件有足夠的空間。 這也是設計師的照片,看起來如何:
我復制您的代碼只是為了查看確切的問題,但一切看起來都很好。 但是,由於空間問題,最后一個EditText狹窄在右側。 您確定您有足夠的空間來容納所有東西嗎?
如果空間太小,則視圖可能會擠在一起。
嘗試以下代碼,它會根據需要對齊:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<TextView
android:id="@+id/userNameLbl"
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="wrap_content"
android:text="Username:" />
<EditText
android:id="@+id/userNameText"
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="wrap_content"
android:minWidth="200dp"
android:text="Sample Text" />
<TextView
android:id="@+id/pwdLbl"
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="wrap_content"
android:text="Password:"
/>
<EditText
android:id="@+id/userNameText2"
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="wrap_content"
android:minWidth="200dp"
android:text="Sample Text" />
</LinearLayout>
如果您需要更多信息,請在下面評論
當您仔細觀察時,在android:layout_toRightOf
和android:layout_toEndOf
屬性中,您使用的是@+id/...
值。 刪除+
以引用現有資源項。
好的,問題似乎出在Xamarin Studio本身中,它不能正確呈現布局的內容。 因為當我在設備上運行此布局時,控制器彼此並排放置,所以沒有這樣的問題,但是設計者卻顯示出完全不同的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.