开发者

Centering a TextView while keeping it to the right of a button?

I'm pretty sure I've done this before, but I've forgotten how.

Here's the problem: I've got a button and a textview, and I want the textview to be centered, while the button is on the left side.

No problem? Just put them in a relativelayout, make the te开发者_开发知识库xtview centerinparent, and the button alignparentleft.

But now I'm going to dynamically change the text, so it can potentially be written on top of the button! I'll just add toRightOf="@id/button" on the textview. No, now it's no longer centered.

I wish I could provide a screenshot, but it seems the computer is out of memory and can't do that.

Here's some code: http://pastebin.com/3N70Vjre (Since I can't paste 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>


Try this (unfortunately I'm at work so can't jump into Eclipse to get you some code) -

  • Change the layout_width of the TextView to fill_parent.
  • Set the gravity of the TextView to center (so the text centers inside the TextView)
  • Set the layout_weight of the Button to 1 and the layout_weight of the TextView to 2. Note that you may have to fudge with these numbers to get the layout you're looking for.

This should center the text of the TextView after the Button, though it will not center the TextView itself. You can accomplish that by replacing the TextView with a container (Linear/Relative Layout) and doing the same method as above on the Layout instead of the TextView. You would then put your TextView inside the container and set the container's gravity to "center".

Hope this helps point you in the right direction :)


You can try this (pseudo-code):

<RelativeLayout>
  <Button>
  <LinearLayout toLeftOf="toptext" type="horizontal">
    <TextView gravity="center">
  </LinearLayout>
</RelativeLayout>

You might have to have the LinearLayout as width="fill_parent". Not sure if that will work nor not. You can subsequently try some of the things listed here: http://thinkandroid.wordpress.com/2010/01/14/how-to-position-views-properly-in-layouts/


Try declaring the TextView first, then aligning the button to the left of the text view. Keep in mind you may run into issues if the TextView becomes too wide.

EDIT: I see, so you're trying to do something sort of like the iPhone's header with back/next buttons (similar anyway). Try this modification. I still believe you're going to run into issues if the TextView gets large enough to hit the Button, though.

  <RelativeLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/header"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      >
      <TextView
          android:id="@+id/toptext"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textColor="@android:color/white"
          android:layout_alignParentCenter="true"
          android:textSize="16sp"
          android:textStyle="bold"
          android:text="Text!"
          android:singleLine="true"
          />
      <Button
          android:id="@+id/leftbutton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:text="text!"
          />        
  </RelativeLayout>

Try this FrameLayout instead. This may do more what you're expecting:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView
        android:id="@+id/toptext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="@android:color/white"
        android:textSize="16sp"
        android:textStyle="bold"
        android:text="Text!"
        android:singleLine="true"
        />
    <Button
        android:id="@+id/leftbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text!"
        />
</FrameLayout>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜