【问题标题】:Switching 2 rows in a tablelayout in android在android中的tablelayout中切换2行
【发布时间】:2011-07-30 19:12:46
【问题描述】:

我在 Android 中有一个这样的表格布局:

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

现在我需要做的是切换 TEXTVIEW/SPINNER。第 2 行中的一个进入第 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);

【问题讨论】:

  • 我将您的旧帐户合并到您的注册帐户中,以便您可以编辑帖子并离开 cmets。

标签: android tablelayout viewflipper viewswitcher


【解决方案1】:

执行此操作的最简单方法是让 Spinner 具有可见性 GONE(不会在容器中发生),其中您有 TextView 和 TextView,您有 Spinner。然后只需切换可见性。这种解决方案并不实用。

【讨论】:

    【解决方案2】:

    老实说,我认为在这种情况下使用 TableLayout 是不切实际的。尽管您提供的视图暗示 TableLayout 是最实用的,但 TableLayout 被设计为相当静态的。我不知道有一种方法可以在 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 以便同时执行两个动画。

    我希望这会有所帮助!如果其中有任何令人困惑的地方,请告诉我,我会尽力解决。

    【讨论】:

      【解决方案3】:

      grmpf,

      让它工作。谢谢。错误是我忘记了这个:

      setFillAfter(true);
      

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

      【讨论】:

        猜你喜欢
        • 2012-01-25
        • 2012-05-27
        • 2023-03-03
        • 2020-12-08
        • 1970-01-01
        • 1970-01-01
        • 2015-11-16
        • 1970-01-01
        • 2015-02-28
        相关资源
        最近更新 更多