0

我有一个相对布局,里面有一个文本视图和另一个按钮

在此处输入图像描述

按钮完美对齐,但我希望 textview 结束应该是它的父中心。 在此处输入图像描述

我的 XML 看起来像

<RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/Tour_Expense_List_Detail_Tbl">

                <Button
                    android:id="@+id/saveButton"
                    style="@style/ButtonText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:background="@drawable/color_button"
                    android:onClick="SaveData"
                    android:text="Save"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/textViewTotalAmtVal"
                    style="@style/ButtonText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:text="Total Amount: "
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="#2E2E2E"
                    android:textStyle="bold" />

            </RelativeLayout>
4

2 回答 2

1

对于你的问题,我有一个解决方案。您可以使用

android:layout_centerInParent="true". 

但这会使您的“总金额”TextView 居中。但是你要你最后一个字符触摸中心点。因此,您可以制作另一个“Blank TextView”并使用 centerInparent 将其放在中心,然后将“TotalAmount”文本视图放在其左侧。

        <TextView
                android:id="@+id/blank_centered_text"
               android:layout_centerInParent="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"></TextView>

            <TextView
                android:id="@+id/textViewTotalAmtVal"
                style="@style/ButtonText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                **android:layout_toLeftOf="@+id/blank_centered_text"**
                android:text="Total Amount: "
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="#2E2E2E"
                android:textStyle="bold" />
于 2013-06-02T10:06:33.630 回答
0

您可以使用LinearLayoutinRelativeLayout然后每个元素(即Button& TextView)都可以使用layout_weight属性。

<RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/Tour_Expense_List_Detail_Tbl">
   <LinearLayout
    android:id="@+id/Row1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <Button
                android:id="@+id/saveButton"
                style="@style/ButtonText"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_weight="1"                     
                android:background="@drawable/color_button"
                android:onClick="SaveData"
                android:text="Save"
                android:textStyle="bold" />

     <TextView
                android:id="@+id/textViewTotalAmtVal"
                style="@style/ButtonText"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_weight="1"                     
                android:text="Total Amount: "
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="#2E2E2E"
                android:textStyle="bold" />
  </LinearLayout>
 </RelativeLayout>
于 2013-06-02T09:43:14.590 回答