1

https://docs.google.com/document/d/1SDDnAG-jkQjC6F85iPgfv3Et1pYl8t03k-TkCN3YcCw/edit如何在嵌套在LinearLayout中的RelativeLayout中實現gravity =「center_vertical」?


我有一個包含股票混合一個Android的首選項頁面,以及自定義列表項。自定義列表項目「情緒」和「混合情緒」可以在引用的Google文檔中的屏幕截圖中看到,並按以下方式運行。 (外部鏈接是因爲沒有10的名譽,我不允許發佈圖像在計算器中...)

1)在用戶選擇一個值之前,他們只顯示他們的標題。 (情緒或混合)

2)用戶選擇一個值後,他們顯示他們的標題和一個自定義工具提示(小彩色正方形),表明他們的選擇。

問題:您可以從屏幕截圖中看到,在用戶選擇一個值之前,「心情」文本不居中。我希望它能夠居中,然後在做出選擇後動態地爲自定義工具提示騰出空間。換句話說,我想

<TextView android:id="@+android:id/title" 

要到

android:gravity="center_vertical". 

這是可能的迴應?

我的佈局文件看起來像這樣。


<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="60dip" 
    android:gravity="center_vertical" 
    android:paddingRight="?android:attr/scrollbarSize"> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_vertical" 
     android:layout_marginLeft="15dip" 
     android:layout_marginRight="6dip"> 

     <TextView android:id="@+android:id/title" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:gravity="center_vertical" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:textSize="18sp"/> 
     <!-- This image represents the dropdown arrow --> 
     <ImageView 
      android:gravity="center_vertical" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_toRightOf="@android:id/title" 
      android:scaleType="fitEnd" 
      android:src="@drawable/ic_btn_round_more_normal_cropped" /> 

     <monarca_rct.client.customcomponents.MoodScalePresentation 
      android:id="@+android:id/hpmp_mood_scale" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@android:id/title" 
      android:paddingTop="4.0dp"/> 

    </RelativeLayout> 

</LinearLayout> 

回答

0

如果我得到正確你的問題:

您可以設置android:layout_alignWithParentIfMissing="true"<TextView android:id="@+android:id/title"更多信息請訪問http://developer.android.com/resources/articles/layout-tricks-efficiency.html

+0

謝謝穆拉!您發佈的「佈局技巧」文章與我想要做的非常相似。不幸的是,閱讀完這篇文章後,再試驗一下,還包括你的'layout_alignWithParentIfMissing'建議,它仍然沒有集中。我很難過... – Colfax

0

我不能確切地嘗試了這一點,但我想這可能是這樣的事情:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="60dip" 
    //REMOVED GRAVITY 
    android:paddingRight="?android:attr/scrollbarSize"> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     //REMOVED GRAVITY 
     android:layout_marginLeft="15dip" 
     android:layout_marginRight="6dip"> 

     <TextView android:id="@+android:id/title" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:gravity="center_vertical|left" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:textSize="18sp"/> 
     <!-- This image represents the dropdown arrow --> 
     <ImageView 
      android:gravity="center_vertical" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_toRightOf="@android:id/title" 
      android:scaleType="fitEnd" 
      android:src="@drawable/ic_btn_round_more_normal_cropped" /> 

     <monarca_rct.client.customcomponents.MoodScalePresentation 
      android:id="@+android:id/hpmp_mood_scale" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@android:id/title" 
      android:paddingTop="4.0dp"/> 

    </RelativeLayout> 

</LinearLayout> 

原因從center_verticalcenter_vertical | left是因爲你的父母佈局就像一個網格,你可以「選擇」你的位置。因此,如果你有:

(1)(2)(3)

(4)(5)(6)

(7)(8)(9)

[唐不介意每行之間的空白,出於某種原因,如果我不這樣做,它會顯示在一行中。]

是您想要文本的位置。正如你所看到的,4對垂直居中因此組合:

android:gravity="center_vertical|left" 

快問,爲什麼你需要的LinearLayout裏面一個RelativeLayout的?從外觀上看,您完全可以不使用LinearLayout。

相關問題