Swap two buttons positions in TableLayout

限于喜欢 提交于 2019-12-24 19:44:24

问题


Suppose I have the following layout

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/TableLayout">
    <TableRow android:id="@+id/TableRow01" >
        <Button android:text="1" android:id="@+id/button01"/>
        <Button android:text="2" android:id="@+id/button02"/>
        <Button android:text="3" android:id="@+id/button03"/>
        <Button android:text="4" android:id="@+id/button04"/>
    </TableRow>
    <TableRow android:id="@+id/TableRow01" >
        <Button android:text="5" android:id="@+id/button05"/>
        <Button android:text="6" android:id="@+id/button06"/>
        <Button android:text="7" android:id="@+id/button07"/>
        <Button android:text="8" android:id="@+id/button08"/>
    </TableRow>
</TableLayout>

How could I swap position of button01 with button02? And would it work also for swapping button01 with button05?

Before asking what I have tried so far, I don't even know where to start. I googled but the only answer I could find was about AbsoluteLayout, which is not what I need.


回答1:


When you say "swap", what do you mean? I suppose you're talking about doing it dynamically, or else you'd just do it in the layout.

What exactly is the purpose of the swapping? Just replacing the text? If that's the case, you could simply call setText() on whichever button you want to change.

EDIT (after clarifying the question via comments): If you want to move them and animate that, then AbsoluteLayout WOULD work, except that it's deprecated. There are other layouts, like using a RelativeLayout and specifying absolute offsets from the top left, or even a GridView. You could then use a TranslateAnimation to move the buttons.



来源:https://stackoverflow.com/questions/3304368/swap-two-buttons-positions-in-tablelayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!