RelativeLayout将居中的View推向右侧

时间:2014-07-14 09:40:25

标签: android android-layout

我希望你能帮助我,因为这让我疯了......

我有一个简单的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 个答案:

答案 0 :(得分:1)

我建议您尝试将自定义TextView的{​​{1}}更改为layout_width,并将其match_parent设置为gravity。这将拉伸center_horizontal以水平覆盖其父级,然后它可以将自己的文本本身放在中心。由于您在TextView之后列出ProgressBar,因此z排序正确,TextView将显示在ProgressBar之上。