Android XML布局对齐(按钮网格)

时间:2013-06-13 06:42:13

标签: android xml android-layout android-linearlayout android-xml

我创建了一组嵌套LinearLayout来组成按钮网格;设置组中每个组和每个孩子的重量,但是对齐方式不会正确。

拥有如此多的嵌套布局似乎并不正确...有更好的方法吗?

这是一个说明我的问题的图片: android xml layout problem

代码很长,所以我只描述大纲:

MAIN LINEAR LAYOUT (horizontal)
   Col 1: linear layout (vertical)
          contains two horizontal linear layouts with two buttons each (2x2 grid)
   Col 2: linear layout (vertical)
          contains three horizontal linear layouts with three buttons each (3x3 grid)
   Col 3: linear layout (vertical)
          contains two horizontal linear layouts with two buttons each (2x2 grid)
END MAIN LINEAR LAYOUT

根据要求here is the XML code

关注@R9j's answer我已切换到RelativeLayout并取得了一些进展。但是,ImageView s重叠存在浮动问题。轻微的尺码问题;我需要很好地将所有内容都装入4.7“Nexus 4屏幕。

Progress with RelativeLayout

2 个答案:

答案 0 :(得分:1)

这会对你有帮助

linearlayout (horizontal){
    relative layout(weight = 1){
        image view(alignParentTop=true, alignParentLeft=true)
        image view(alignParentTop=true, alignParentRight=true)
        image view(alignParentBottom=true, alignParentLeft=true)
        image view(alignParentBottom=true, alignParentRight=true)
    }
    relative layout(weight = 1){
        image view(alignParentLeft=true, height=fillParent)
        image view(alignParentRight=true, height=fillParent)
        image view(alignParentTop=true, centerHorizontal=true)
        image view(alignParentBottom=true, centerHorizontal=true)
    }
    relative layout(weight = 1){
        image view(alignParentTop=true, alignParentLeft=true)
        image view(alignParentTop=true, alignParentRight=true)
        image view(alignParentBottom=true, alignParentLeft=true)
        image view(alignParentBottom=true, alignParentRight=true)
    }
}

根据上述模型设计你的xml。

答案 1 :(得分:0)

您可以尝试GridLayout

看起来你也可以在RelativeLayout中这样做,只需要更加小心。

编辑:如果您遇到问题,请发布您的XML文件,如果我有时间,我会重做并发布解决方案xml。