我很确定我以前做过,但我忘记了如何做。
这是问题所在:我有一个按钮和一个文本视图,我希望文本视图居中,而按钮位于左侧。
没问题?只需将它们放在相对布局中,使 textview centerinparent 和 button alignparentleft。
但现在我要动态更改文本,所以它可能会写在按钮的顶部!我将在 textview 上添加 toRightOf="@id/button" 。不,现在它不再居中。
我希望我能提供一个屏幕截图,但似乎计算机内存不足,无法做到这一点。
这是一些代码: http: //pastebin.com/3N70Vjre(因为我不能粘贴 xml ......?)
  <RelativeLayout
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true">
        <Button
                android:id="@+id/leftbutton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:text="text!"
                />
        <TextView
                android:id="@+id/toptext"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@android:color/white"
                android:layout_toRightOf="@id/leftbutton"
                android:layout_centerInParent="true"
                android:textSize="16sp"
                android:textStyle="bold"
                android:text="Text!"
                android:singleLine="true"
                />
  </RelativeLayout>