在android中的tablelayout中切换2行

时间:2011-07-27 17:58:23

标签: android tablelayout viewflipper viewswitcher

我在Android中有这样的tablelayout:

1) Row == IMAGEVIEW | TEXTVIEW
2) Row == IMAGEVIEW | SPINNER

现在我需要做的是切换TEXTVIEW / SPINNER。第2行的第1行转到第1行,1的第1行转到第2行。

拥有一个小动画也很棒。我见过Viewswitcher和Viewflipper,但这似乎不是我想要的。任何人都知道如何让它发挥作用?

我的布局(一部分)看起来像这样:

<TableRow>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/left_layout_controlls"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
                <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="wrap_content" 
                    android:stretchColumns="*"
                    android:id="@+id/top_controlls"
                    android:layout_height="wrap_content">

                    <TableRow>
                        <ImageView
                           android:id="@+id/fromCountry_img" 
                           android:src="@drawable/tc_rt_from"
                           android:layout_width="wrap_content"
                           android:layout_gravity="center_horizontal"
                           android:layout_height="fill_parent"
                           />

                        <TextView
                            android:id="@+id/fromCountry"
                            android:layout_marginTop="2px"
                            android:layout_marginLeft="2px"
                            android:background="@drawable/round_edges_main_controll"

                            android:layout_marginRight="2px"
                            android:layout_height="38px"
                            android:layout_width="160dip"/>
                        </TableRow>
                     </TableLayout>

                    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
                        android:layout_width="wrap_content" 
                        android:stretchColumns="*"
                        android:layout_below="@id/top_controlls"
                        android:layout_height="wrap_content">
                        <TableRow>
                             <ImageView 
                               android:src="@drawable/tc_rt_to"
                               android:layout_width="wrap_content"
                               android:layout_gravity="center_horizontal"
                               android:layout_height="fill_parent"
                               android:layout_below="@id/fromCountry_img"
                               />

                            <Spinner 
                                android:id="@+id/toCountry"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="2px"
                                android:layout_marginBottom="2px"
                                android:layout_marginRight="2px"
                                android:layout_width="160dip"
                                android:layout_weight="1"
                                android:drawSelectorOnTop="true"/>
                        </TableRow>
                    </TableLayout>

更新

我试图像这样切换控件,并且动画可以工作,但是当动画结束时,控件将跳回原来的位置。

知道为什么吗?

LinearLayout layout1 = ((LinearLayout) DataHolder.activityHolder.findViewById(R.id.top_controll));
            LinearLayout layout2 = ((LinearLayout) DataHolder.activityHolder.findViewById(R.id.bottom_controll));
DataHolder.activityHolder.findViewById(R.id.toCountry));
TranslateAnimation a = new TranslateAnimation(
Animation.ABSOLUTE,0,Animation.ABSOLUTE,0,
Animation.ABSOLUTE,0,Animation.ABSOLUTE, 40);
a.setDuration(1200);

TranslateAnimation b = new TranslateAnimation(
Animation.ABSOLUTE,0,Animation.ABSOLUTE,0,
Animation.ABSOLUTE,0,Animation.ABSOLUTE, -40);
b.setDuration(1200);

layout1.startAnimation(a);
layout2.startAnimation(b);

3 个答案:

答案 0 :(得分:0)

最简单的方法是让Spinner具有可见性GONE(不会在容器中发生),在那里你有TextView和TextView,你有Spinner。然后只需切换可见性。这个解决方案不实用。

答案 1 :(得分:0)

老实说,我认为在这种情况下使用TableLayout是不切实际的。虽然您提供的视图暗示TableLayout是最实用的,但TableLayouts设计为相当静态。我不知道有一种方法可以在TableLayout中移除或动画化TableRows。

我建议使用以下内容:

<LinearLayout android:orientation="vertical" ... >
    <LinearLayout android:orientation="horizontal" ... > 
        CONTENTLAYOUTS
    </LinearLayout> 
    ... MORE LINEARLAYOUTS FOR MORE ROWS
</>

然后你可以操纵这些项目的位置。

例如,您可以存储当前位置:

LinearLayout layout1 = (LinearLayout) findViewById(R.id.layout1);
int position1 = layout1.getTop();

LinearLayout layout2 = (LinearLayout) findViewById(r.id.layout2);
int position2 = layout2.getTop();

AnimationSet animSet = new AnimationSet(...);
//define your animations here, or load them from resources
//to move each view to each of the two positions, changing the 
//Y coordinate of the Views

确保使用AnimationSet以便同时执行两个动画。

我希望这有帮助!如果有任何混淆,请告诉我,我会尽力清理它。

答案 2 :(得分:0)

grmpf,

让它发挥作用。谢谢。错误是我忘了这个:

setFillAfter(true);

这将在动画结束后保持更改

相关问题