权重平等分布局And TableRow布局误区

来源:互联网 发布:iphone电池检测软件 编辑:程序博客网 时间:2024/05/01 23:35

开头语:

本人最近在自学Android,虽然本人有2年Java Web的开发经验。但是发现Android的自学之路并不是那么平坦,我没有Android真机。但是有一个window phone的手机。开始想做一个通讯录。但是没有参考软件。这样一来我参考wp平台的通讯录去开发本软件,这其中遇到了一些关于sqlite、键盘布局等问题。本文说一说布局的权重问题吧


个人理解:

权重是一个在指定组件内等分该组件空间的一种手段。在android中的设置方式为android:layout_weight="X"


本人案例截图:


说明:

如上图我做的键盘排版每四个按照为一行。详细的布局为4个按钮在一个LinearLayout中。那么只要将按钮的权重都设置为1即可。

那么就会把父元素(LinearLayout)组件4等分。[因为这里有四个按钮。当然有2个按钮则2等分。我这么说我想大家应该都可以理解了吧]


部分代码解析:

<LinearLayout        android:id="@string/l1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="50dip"        android:orientation="horizontal" >        <Button            android:id="@+id/buttonjing"            android:layout_width="match_parent"            android:layout_height="50dip"            android:layout_weight="1"            android:text="@string/jing" />        <Button            android:id="@+id/buttonA"            android:layout_width="match_parent"            android:layout_height="50dip"            android:layout_weight="1"            android:text="@string/A" />        <Button            android:id="@+id/buttonB"            android:layout_width="match_parent"            android:layout_height="50dip"            android:layout_weight="1"            android:text="@string/B" />        <Button            android:id="@+id/buttonC"            android:layout_width="match_parent"            android:layout_height="50dip"            android:layout_weight="1"            android:text="@string/C" />    </LinearLayout>

说明:

OK,上面代码为从#号到C按钮一行的排版。这一行的排版。可以看到4个btn的属性的权重都为1.其中width最好是设置上。


误区说明:

在尝试LinearLayout布局此界面之前我用过TableLayout和TableRow去搭配。虽然TableRow组件也可以用权重4等分。但是发现其高度无法调整。总是占用手机屏幕的半屏。再次说明一下。避免再犯!!

原创粉丝点击