嵌套布局的麻烦

时间:2010-12-01 19:17:37

标签: android layout

我刚刚开始使用Android并且让自己陷入困境。我做了一个简单的应用程序/视图。这个想法是在顶部画一个线迷宫,底部有动作按钮。我的应用程序只在框架中绘制一条带有几条线的x,但这样可行。问题是我的按钮被绘制得太远了,并被剪裁了高度。

我认为问题全在我的布局xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

        <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" > 

            <none.maze.MazeView
             android:id="@+id/maze"
                android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        />

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >
                <TextView
                 android:id="@+id/text"
                    android:text="@string/maze_layout_text_text"
                    android:visibility="visible"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:gravity="center_horizontal"
                    android:textColor="#ff8888ff"
                    android:textSize="24sp"/>
            </RelativeLayout>

        </FrameLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >             

            <Button
                android:id="@+id/left"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Left" 
                android:layout_weight="1" 
                />

            <Button
                android:id="@+id/forward"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Forward" 
                android:layout_weight="1" 
                />

            <Button
                android:id="@+id/right"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Right" 
                android:layout_weight="1" 
                />

        </LinearLayout>

</LinearLayout>

3 个答案:

答案 0 :(得分:1)

我认为你不应该同时使用layout_height="fill_parent"layout_weight。尝试为layout_height="0dip"设置layout_weight="1"FrameLayout,为layout_height="wrap_content"设置LinearLayout。较低的LinearLayout不得具有layout_weight属性。

答案 1 :(得分:0)

使用Hierarchy Viewer调试布局。非常酷。

答案 2 :(得分:0)

你的整个布局太过臃肿无效。您的使用方式太多ViewGroup。我建议您阅读RelativeLayout s,特别是整个Layout Tricks系列。