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

开头语:

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

个人理解:

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

 

本人案例截图:


说明:

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

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

部分代码解析:

  1. <LinearLayout  
  2.         android:id="@string/l1"  
  3.         android:layout_width="match_parent"  
  4.         android:layout_height="wrap_content"  
  5.         android:layout_marginTop="50dip"  
  6.         android:orientation="horizontal" >  
  7.   
  8.         <Button  
  9.             android:id="@+id/buttonjing"  
  10.             android:layout_width="match_parent"  
  11.             android:layout_height="50dip"  
  12.             android:layout_weight="1"  
  13.             android:text="@string/jing" />  
  14.   
  15.         <Button  
  16.             android:id="@+id/buttonA"  
  17.             android:layout_width="match_parent"  
  18.             android:layout_height="50dip"  
  19.             android:layout_weight="1"  
  20.             android:text="@string/A" />  
  21.   
  22.         <Button  
  23.             android:id="@+id/buttonB"  
  24.             android:layout_width="match_parent"  
  25.             android:layout_height="50dip"  
  26.             android:layout_weight="1"  
  27.             android:text="@string/B" />  
  28.   
  29.         <Button  
  30.             android:id="@+id/buttonC"  
  31.             android:layout_width="match_parent"  
  32.             android:layout_height="50dip"  
  33.             android:layout_weight="1"  
  34.             android:text="@string/C" />  
  35.     </LinearLayout>  

说明:

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

 

误区说明:

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

原文地址:https://www.cnblogs.com/vijozsoft/p/4511686.html