0

我想将我的文本设置在视图的中心。我知道我需要用 Gravity 来做,但仍然看不到正确的结果我只想做从中心下方开始向左向右并返回开始位置的动画我的代码是

        setContentView(R.layout.logo);
        mTextView = (TextView) findViewById(R.id.textLabel);
    mLayout = new LinearLayout(this); 
        mLayout.setGravity(Gravity.CENTER); 
        mTextView.setGravity(Gravity.AXIS_X_SHIFT/2); 
        mTextView.setGravity(Gravity.AXIS_Y_SHIFT/2-Gravity.AXIS_Y_SHIFT/3); 
        mAnimation = new TranslateAnimation(100f, -100f, 0.0f, 0.0f); 
        mAnimation.setDuration(2000); 
        mTextView.setAnimation(mAnimation);

        mAnimation.start();

xml看起来像:

 <ViewFlipper android:id="@+id/flipper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:flipInterval="2000"
        android:layout_marginBottom="20dip" >
<LinearLayout
android:id="@+id/Linear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/logonews"
android:baselineAligned="false"
android:orientation="horizontal" >

<TextView
    android:id="@+id/textLabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:text="Israel News"
    android:textSize="18sp"
    android:textStyle="bold"

     />

  </LinearLayout>
  </ViewFlipper>

感谢帮助

4

2 回答 2

1

When do you want the Animation to play?

What result are you seeing instead?

If you want it to play immediately, use startAnimation instead of setAnimation.

Edit: Looking at your code in light of the comments you made...

If you want your TextView to be below your logo image, you'll need to remove the background drawable from the LinearLayout, change its orientation to vertical, and put the image into an ImageView instead, before your TextView in the LinearLayout. This allows Android to lay them out in order...

Like this:

 <ViewFlipper android:id="@+id/flipper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:flipInterval="2000"
        android:layout_marginBottom="20dip" >
<LinearLayout
android:id="@+id/Linear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:orientation="vertical" >

<ImageView
    android:src="@drawable/logonews"
    android:scaleType="fitCenter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
>


<TextView
    android:id="@+id/textLabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:text="Israel News"
    android:textSize="18sp"
    android:textStyle="bold"

     />

  </LinearLayout>
  </ViewFlipper>

or if you need the image to be the background you can create an empty View above the TextView that's the same size as the image.

于 2012-04-13T14:42:48.493 回答
0
  • 1、Gravity 属性对TextView 中文本内容的位置有影响,而不是TextView。
  • 2、TranslateAnimation 只直线移动一次。它不会前后移动,所以你可能需要结合几个 TranslateAnimation 来实现你想要的。
  • 3、用户startAnimation。
于 2012-04-13T15:11:27.833 回答