將textView和editText作爲一行放在屏幕中間

在編寫過程中,會出現文本框和編輯框應該在同一行,但是進行調整時經常會錯位的情況。

如果用RelativeLayout的佈局方式,必然有一個要作爲基準,另一個根據基準進行調整。這樣的壞處是經常會“牽一髮而動全身”,而且調整起來不太方便。並且,不同手機的屏幕尺寸不同,如果基準控件確定了絕對位置時,在不同屏幕上看起來有時會在中間,有時會靠邊。爲了使得在不同機子的屏幕都能夠出現在中間,可以考慮將文本框和編輯框作爲一個整體進行處理,這樣就可以利用語句使得他們位於屏幕中間。


方法:外面包一層橫向linearlayout

e.g.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
 
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="LeftButton" />
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" >
 
        </LinearLayout>
 
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RightButton" />
    </LinearLayout>
這樣就可以將文本框和編輯框都水平放置,如果要放在水平中間,在orientation那句話後面加上: android:layout_centerHorizontal="true"


參考http://bbs.csdn.net/topics/390458994

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章