与布局外的对象对齐

时间:2016-12-12 23:03:03

标签: android android-layout

我是Android的新手,我一直试图为我想出的抽象游戏制作界面。

我有一块5x5电池的电路板,我有这些电池的瓷砖。 我想制作动画,其中瓷砖从屏幕顶部落到它们在单元格中的位置,所以我一直在尝试将单元格内的瓷砖与最顶层的布局对齐。

我知道我可以将tile代码放在最底部的布局中,单元格本身,但是如果我这样做,它们就没有正确设置动画,因为元素只显示在其父级的约束内。

这是我的XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eeeeee"
tools:context="com.example.althis.testsequence.FullscreenActivity">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="@drawable/rectangle"
    >
    <Button
        android:id="@+id/tile1"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:backgroundTint="@color/tile_color"
        android:text="1"
        android:textColor="@color/white"
        android:textSize="35dp"/>

    <com.example.me.testsequence.Square_Linear_Layout
        android:layout_width="match_parent"
        android:layout_height="380dp"
        android:layout_marginTop="70dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/rounded_rectangle">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="8dp"
            android:background="@drawable/space"
            android:orientation="vertical">
            <LinearLayout
                android:layout_margin="1dp"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1.0"
                    android:orientation="horizontal"
                    android:layout_margin="0dp">


                    <LinearLayout
                        android:id="@+id/a1"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="1.0"
                        android:orientation="horizontal"
                        android:background="@drawable/space">

                        <Button
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:backgroundTint="@color/tile_color"
                            android:text="1"
                            android:textColor="@color/white"
                            android:textSize="35dp"/>
                    </LinearLayout>

                </LinearLayout>

                <LinearLayout>
                </LinearLayout>

                <LinearLayout>
                </LinearLayout>

                <LinearLayout>
                </LinearLayout>

                <LinearLayout>
                </LinearLayout>

            </LinearLayout>
        </LinearLayout>
    </com.example.althis.testsequence.Square_Linear_Layout>
</RelativeLayout>

我删除了大部分重复代码的易读性,但最底层的布局每行重复五次。 a1单元格中的按钮只是为了举例说明我之前尝试过的方式。按钮正确对齐,但正如我所提到的,它没有正确动画。

那么,是否有更智能的方法将按钮与较低布局上的对象对齐?

1 个答案:

答案 0 :(得分:0)

我知道你是android dev的新手,但你的布局层次结构对于你想要解决的问题来说过于复杂。层次结构的最内层视图是8级深度,具有多个LinearLayouts。请先查看https://developer.android.com/training/improving-layouts/optimizing-layout.html。这在处理动画时非常重要。更深层次的层次结构需要指数级更多的帧时间来布局不同的视图。如果在动画期间更改视图的大小/位置,则最好在16ms帧时间内发生。

现在为瓷砖制作动画。使用传统的视图层次结构来绘制板,您只能为一块瓷砖制作动画片#34;在父层次结构的范围内。如果您想从顶部删除项目,这意味着您的父层次结构必须完全匹配您的窗口大小。这样做会导致电路板明显偏离中心。所以自定义ViewGroup就是答案。您可以通过计算子视图的x / y坐标及其宽度和高度来手动布局子视图。

自定义ViewGroups上的一个很好的tutoral:http://stacktips.com/tutorials/android/how-to-create-custom-layout-in-android-by-extending-viewgroup-class。不幸的是,它是android开发中的一个更高级的主题。我强烈建议你从简单开始,尽可能使用动画,一旦你有工作结果,就开始添加眼睛糖果。

如果你试图实现每一个花哨的动画,你很可能永远不会真正完成你的项目。