5

我有TableRow一个TextView。这是它的xml。

<TableRow
    android:layout_height="fill_parent" 
    android:layout_gravity="bottom"
    android:layout_width="fill_parent"
    android:background="#BF000000">

    <TextView
        android:id="@+id/topText"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF"
        android:textSize="19sp"
        android:background="#BF000000"
        android:layout_gravity="center_horizontal"
        android:text="@string/text_searchword"
        android:layout_width="fill_parent">
    </TextView>

</TableRow>

我想通过按钮触摸的淡出效果使表格行不可见,反之亦然。我该怎么做?

4

1 回答 1

16

任何ViewTableRow包括)都可以附加一个淡入淡出动画,但您需要能够在代码中引用您的视图,因此该行需要一个 id:

<TableRow
  android:id="@+id/my_row"
  android:layout_height="fill_parent" 
  android:layout_gravity="bottom"
  android:layout_width="fill_parent"
  android:background="#BF000000">
  <TextView
    android:id="@+id/topText"
    android:layout_height="wrap_content"
    android:textColor="#FFFFFF"
    android:textSize="19sp"
    android:background="#BF000000"
    android:layout_gravity="center_horizontal"
    android:text="@string/text_searchword"
    android:layout_width="fill_parent">
  </TextView>
</TableRow>

现在您可以在 Java 代码中的某处(onCreate()可能)引用该行本身

View row = findViewById(R.id.my_row);

请注意,我不会将其转换为TableRow. 如果您需要用它做其他事情,您可以这样做,但只是设置可见性,将其保留为视图就可以了。然后只需构造一个按钮单击方法,如下所示:

public void onClick(View v) {
    View row = findViewById(R.id.myrow);
    if(row.getVisibility() == View.VISIBLE) {
        row.startAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
        row.setVisibility(View.INVISIBLE);
    } else {
        row.startAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
        row.setVisibility(View.VISIBLE);
    }
}

Fade in 和 Fade out 是 Android 包中定义的标准动画,您不需要自己创建它们,只需使用AnimationUtils.loadAnimation(). 在这个例子中,点击同一个按钮只是在淡入和淡出之间切换,这取决于视图是否已经可见。

希望有帮助!

于 2011-04-08T13:17:32.197 回答