简体   繁体   English

在TableLayout中交换两个按钮位置

[英]Swap two buttons positions in TableLayout

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 ? 如何将button01位置button01 button02 And would it work also for swapping button01 with button05 ? 并且它也可以将button01button05交换吗?

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. 我用谷歌搜索,但我唯一能找到的答案是关于AbsoluteLayout ,这不是我所需要的。

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. 如果是这种情况,您可以简单地在要更改的任何按钮上调用setText()

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. 编辑(通过注释澄清问题后):如果要移动它们并设置动画效果,则AbsoluteLayout可以工作,但已弃用。 There are other layouts, like using a RelativeLayout and specifying absolute offsets from the top left, or even a GridView. 还有其他布局,例如使用RelativeLayout并从左上角指定绝对偏移,甚至是GridView。 You could then use a TranslateAnimation to move the buttons. 然后,您可以使用TranslateAnimation移动按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM