2013-02-08 30 views
1

截圖張貼如下。在每一行中,最後3列不是垂直居中在它們的行中。直到屏幕顯示之後,每行的高度才知道。我不確定,但我猜我需要在顯示所有內容之後動態地查找該行的高度,然後將所有內容居中。這是正確的路線嗎?中心TextViews/ImageViews垂直未知高度 - Android

下面是這個類的XML代碼的一個截圖。

<ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight="65" 
     android:fillViewport="true" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 

      <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal" > 

       <ImageView 
        android:id="@+id/q1Image" 
        android:layout_width="10dp" 
        android:layout_height="10dp" /> 

       <TextView 
        android:id="@+id/q1Question" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:textSize="10sp" 
        android:layout_weight="48" 
        android:paddingLeft="5dp" 
        android:paddingBottom="6dp" /> 

       <TextView 
        android:id="@+id/q1Answer" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:textSize="10sp" 
        android:layout_weight="27" 
        android:paddingBottom="6dp" /> 

       <TextView 
        android:id="@+id/q1Verse" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:textSize="10sp" 
        android:layout_weight="25" 
        android:paddingBottom="6dp" /> 
      </LinearLayout> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="1dp"  
      android:background="#C2BEBF" /> 

      <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal" > 

       <ImageView 
        android:id="@+id/q2Image" 
        android:layout_width="10dp" 
        android:layout_height="10dp" /> 

       <TextView 
        android:id="@+id/q2Question" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:textSize="10sp" 
        android:layout_weight="48" 
        android:paddingLeft="5dp" 
        android:paddingBottom="6dp" 
        android:paddingTop="6dp" /> 

       <TextView 
        android:id="@+id/q2Answer" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:textSize="10sp" 
        android:layout_weight="27" 
        android:paddingBottom="6dp" 
        android:paddingTop="6dp" /> 

       <TextView 
        android:id="@+id/q2Verse" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:textSize="10sp" 
        android:layout_weight="25" 
        android:paddingBottom="6dp" 
        android:paddingTop="6dp" /> 
      </LinearLayout> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="1dp"  
      android:background="#C2BEBF" /> 

      <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal" > 

       <ImageView 
        android:id="@+id/q3Image" 
        android:layout_width="10dp" 
        android:layout_height="10dp" /> 

       <TextView 
        android:id="@+id/q3Question" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:textSize="10sp" 
        android:layout_weight="48" 
        android:paddingLeft="5dp" 
        android:paddingBottom="6dp" 
        android:paddingTop="6dp" /> 

       <TextView 
        android:id="@+id/q3Answer" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:textSize="10sp" 
        android:layout_weight="27" 
        android:paddingBottom="6dp" 
        android:paddingTop="6dp" /> 

       <TextView 
        android:id="@+id/q3Verse" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:textSize="10sp" 
        android:layout_weight="25" 
        android:paddingBottom="6dp" 
        android:paddingTop="6dp" /> 
      </LinearLayout> 

//Rows 4-10 code excluded because of repetition. 

enter image description here

+0

老兄,你應該在你的XML中使用ListView代替這個靜態的LinearLayout。 – Sean 2013-02-08 17:33:52

+0

我不熟悉ListView。你能詳細說明一下嗎? – Matt 2013-02-08 17:36:17

+1

ListView(和他的兄弟爲「AdapterView」系列:AdapterViewFlipper,ExpandableListView,Gallery,GridView,ListView,Spinner,StackView)是一個視圖,其目的是在運行時填充項目,當您有大量行或當你只在運行時知道他們的內容。 ListView爲您提供了一種簡單的方法來定義列表中的一個項目,然後用重複的項目填充該列表,這是在Adapter的幫助下完成的。閱讀更多@ web,這太棒了! – Sean 2013-02-08 17:40:43

回答

4

集安卓layout_gravity到center_vertical你的文本視圖。

基本上,因爲你的layout_height設置爲包裹內容,文字的看法是這樣的:

------- 
content 
------- 

文本視圖與其父對齊,像這樣:

------- 
------- 
content 
------- 




------- 

你的目標是讓視圖垂直居於母體中心。屬性「layout_gravity」告訴視圖如何在其父代中調整自己。因此,設置到layout_gravity center_vertical將告訴視圖到其父項內居中,像這樣:

------- 


------- 
content 
------- 


------- 

可以完成此的另一種方式是用「重力」屬性。 「重力」屬性告訴父母如何佈置其內容(與「layout_gravity」相反,後者定義告訴視圖如何坐在其父代中)。因此,另一種可能的解決方案是告訴列表視圖將其所有內容垂直對齊。這是通過在ViewGroup上指定android:gravity =「center_vertical」來完成的(在你的情況下是LinearLayout)。

+0

每個''''TextView''''的高度是否與整行相同,因此將整行放在中間以使它看起來不錯?例如,在屏幕截圖的第2行中,一個''''TextView''''是3行高,其他2行是1行。 – Matt 2013-02-08 17:31:46

+1

如果我理解你的問題是正確的,你想知道如何垂直居中放置文本。我將更新我的答案,詳細介紹如何將內容集中,同時您應該嘗試指定我在答案中提到的那個屬性。 – WindyB 2013-02-08 17:33:24

+0

我現在要這樣做。 – Matt 2013-02-08 17:35:05