2014-07-14 21 views
0

我希望你能幫助我,因爲這是推動我堅果...RelativeLayout的推壓中心查看到右

我有一個簡單RelativeLayout有兩個子視圖,你可以將代碼樣本中看到。 TextView應該是居中ProgressBar左對齊

在佈局編輯器中,這是正確渲染的,但在設備/仿真器上TextView被推到右邊,並且ProgressBar也有一個隨機偏移量。

你可以忽略文本和按鈕顏色不同的事實,我已經檢查過這沒有什麼區別。

<RelativeLayout 
    android:id="@+id/popup_redeem_button" 
    android:layout_margin="@dimen/margin_outer" 
    android:background="@drawable/button_rounded_yellow" 
    android:clickable="true" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <de.fireflow.milebox.ui.views.TypefaceTextView 
     android:id="@+id/popup_redeem_text" 
     style="@style/ButtonHeadlineMenu_Font" 
     app:richText="true" 
     android:padding="@dimen/margin_normal" 
     android:text="@string/voucher_redeem" 
     android:layout_centerInParent="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

    <ProgressBar 
     android:id="@+id/progressBar" 
     style="?android:attr/progressBarStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:indeterminate="true" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="@dimen/margin_normal" 
     android:indeterminateBehavior="cycle" 
     android:layout_gravity="center_horizontal"/> 
    </RelativeLayout> 

形狀的按鈕背景:

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <corners 
    android:radius="6dp" 
    /> 

    <solid 
    android:color="@color/btn_yellow_pressed" 
    /> 
</shape> 

enter image description here

我不衡量或更改任何佈局界限的代碼,所以我真的不認爲這個問題的。有任何想法嗎?

+1

嘗試將自定義'TextView'的'layout_width'更改爲''match_parent「',並將其'gravity'設置爲'center_horizo​​ntal'。 –

+1

謝謝,這真的解決了這個問題。簡單但高效:D。如果你添加它作爲答案,我會接受它 – McLovin

+0

哦,neato!我會。謝謝! –

回答

1

我建議您嘗試更改自定義TextViewlayout_widthmatch_parent,並將gravity設置爲center_horizontal。這將拉伸TextView以水平覆蓋父級,然後它可以自行將自己的文本居中。由於您在TextView之後列出了ProgressBar,所以z順序將是正確的,並且ProgressBar將出現在TextView之上。