[英]Vertically spacing TextViews in LinearLayout
我有一個LinearLayout,我想垂直添加3個TextView。 我能做的。
我的問題是:如何使TextViews在我的LinearLayout(恰好是800x600像素)內均勻地垂直隔開。
假設LinearLayout高為600像素,這意味着將3行文本定位在y = 150,y = 300,y = 450行上(大約)。
我將如何創建XML文件來實現垂直間距?
注意:我不想使用絕對坐標。
您可以通過在TextView
之間和周圍放置空View
,並設置它們的高度和權重以使它們平均填充剩余空間來實現此目的。 在您的示例中,高度為600,這將使它們分別為150、300和450,而不是像對TextView
那樣加權的情況下分別為TextView
和500。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
您可以使用weight屬性使TextView的縮放比例達到窗口的大小。 此外,您可以指定重力屬性,以使TextView
內的文本居中。
在此代碼中,每個TextView
占據屏幕的1/3:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
android:layout_weight="1"
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:gravity="center"
android:text="tv1"/>
<TextView
android:id="@+id/tv2"
android:layout_weight="1"
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:gravity="center"
android:text="tv1"/>
<TextView
android:id="@+id/tv3"
android:layout_weight="1"
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:gravity="center"
android:text="tv1"/>
</LinearLayout>
下面是均勻管理您的文本視圖的代碼,只需復制粘貼即可
當您在XML代碼中使用重量和重力進行設計時,它將在所有屏幕上自動調整
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#ffffff"
android:text="1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#ffffff"
android:text="2"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#ffffff"
android:text="3"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" />
</LinearLayout>
您可以在layout_weight
的幫助下進行
樣品
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="text1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="text2"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="text3"
android:textAppearance="?android:attr/textAppearanceLarge" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.