I am going to implement the Calculator type application. In that I have set the Different but开发者_如何学Cton as like below code:
<RelativeLayout android:id="@+id/linear_layout" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:orientation="vertical"
android:layout_centerInParent="true" android:gravity="center_horizontal">
<!-- First row Start -->
<Button android:id="@+id/sevenNumber"
android:layout_height="wrap_content" android:layout_width="50dp"
android:text="7" android:textColor="#ffffff" android:textSize="22dp"
android:layout_marginLeft="5dp" android:layout_marginRight="5dp"
android:layout_marginTop="5dp" android:layout_marginBottom="5dp"
android:background="@drawable/selector_button"/>
<Button android:id="@+id/eightNumber" android:layout_toRightOf="@+id/sevenNumber"
android:layout_height="wrap_content" android:layout_width="50dp"
android:text="8" android:textColor="#ffffff" android:textSize="22dp"
android:background="@drawable/selector_button"
android:layout_marginTop="5dp" android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp" android:layout_marginRight="5dp"/>
<Button android:id="@+id/nineNumber" android:layout_toRightOf="@+id/eightNumber"
android:layout_height="wrap_content" android:layout_width="50dp"
android:text="9" android:textColor="#ffffff" android:textSize="22dp"
android:layout_marginLeft="5dp" android:layout_marginRight="5dp"
android:layout_marginTop="5dp" android:layout_marginBottom="5dp"
android:background="@drawable/selector_button"/>
<Button android:id="@+id/acButton" android:layout_toRightOf="@+id/nineNumber"
android:layout_height="wrap_content" android:layout_width="50dp"
android:text="AC" android:textColor="#ffffff" android:textSize="22dp"
android:layout_marginLeft="5dp" android:layout_marginRight="5dp"
android:layout_marginTop="5dp" android:layout_marginBottom="5dp"
android:background="@drawable/brown_button"/>
<!-- First row Finish -->
<!-- Second row Start -->
<Button android:id="@+id/fourNumber" android:layout_below="@+id/sevenNumber"
android:layout_height="wrap_content" android:layout_width="50dp"
android:text="4" android:textColor="#ffffff" android:textSize="22dp"
android:layout_marginLeft="5dp" android:layout_marginRight="5dp"
android:layout_marginTop="5dp" android:layout_marginBottom="5dp"
android:background="@drawable/selector_button"/>
<Button android:id="@+id/fiveNumber" android:layout_toRightOf="@+id/fourNumber" android:layout_below="@+id/eightNumber"
android:layout_height="wrap_content" android:layout_width="50dp"
android:text="5" android:textColor="#ffffff" android:textSize="22dp"
android:background="@drawable/selector_button"
android:layout_marginLeft="5dp" android:layout_marginRight="5dp"
android:layout_marginTop="5dp" android:layout_marginBottom="5dp"/>
<Button android:id="@+id/sixNumber" android:layout_toRightOf="@+id/fiveNumber" android:layout_below="@+id/nineNumber"
android:layout_height="wrap_content" android:layout_width="50dp"
android:text="6" android:textColor="#ffffff" android:textSize="22dp"
android:layout_marginLeft="5dp" android:layout_marginRight="5dp"
android:layout_marginTop="5dp" android:layout_marginBottom="5dp"
android:background="@drawable/selector_button"/>
<Button android:id="@+id/crearButton" android:layout_toRightOf="@+id/sixNumber" android:layout_below="@+id/acButton"
android:layout_height="wrap_content" android:layout_width="50dp"
android:text="C" android:textColor="#ffffff" android:textSize="22dp"
android:layout_marginLeft="5dp" android:layout_marginRight="5dp"
android:layout_marginTop="5dp" android:layout_marginBottom="5dp"
android:background="@drawable/brown_button"/>
<!-- Second row finish -->
<!-- Third row Start -->
<Button android:id="@+id/firstNumber" android:layout_below="@+id/fourNumber"
android:layout_height="wrap_content" android:layout_width="50dp"
android:text="1" android:textColor="#ffffff" android:textSize="22dp"
android:layout_marginLeft="5dp" android:layout_marginRight="5dp"
android:layout_marginTop="5dp" android:layout_marginBottom="5dp"
android:background="@drawable/selector_button"/>
<Button android:id="@+id/secondNumber" android:layout_toRightOf="@+id/firstNumber" android:layout_below="@+id/fiveNumber"
android:layout_height="wrap_content" android:layout_width="50dp"
android:text="2" android:textColor="#ffffff" android:textSize="22dp"
android:background="@drawable/selector_button"
android:layout_marginLeft="5dp" android:layout_marginRight="5dp"
android:layout_marginTop="5dp" android:layout_marginBottom="5dp"/>
<Button android:id="@+id/threeNumber" android:layout_toRightOf="@+id/secondNumber" android:layout_below="@+id/sixNumber"
android:layout_height="wrap_content" android:layout_width="50dp"
android:text="3" android:textColor="#ffffff" android:textSize="22dp"
android:layout_marginLeft="5dp" android:layout_marginRight="5dp"
android:layout_marginTop="5dp" android:layout_marginBottom="5dp"
android:background="@drawable/selector_button"/>
<!-- Third row finish -->
<!-- Fourth row Start -->
<Button android:id="@+id/zeroNumber" android:layout_below="@+id/firstNumber"
android:layout_height="wrap_content" android:layout_width="110dp"
android:text="0" android:textColor="#ffffff" android:textSize="22dp"
android:layout_marginLeft="5dp" android:layout_marginRight="5dp"
android:layout_marginTop="5dp" android:layout_marginBottom="5dp"
android:background="@drawable/selector_button"/>
<Button android:id="@+id/dotNumber" android:layout_toRightOf="@+id/zeroNumber" android:layout_below="@+id/threeNumber"
android:layout_height="wrap_content" android:layout_width="50dp"
android:text="." android:textColor="#ffffff" android:textSize="22dp"
android:background="@drawable/selector_button"
android:layout_marginTop="5dp" android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp" android:layout_marginRight="5dp"/>
<Button android:id="@+id/doNothing" android:layout_toRightOf="@+id/threeNumber" android:layout_below="@+id/crearButton"
android:layout_height="wrap_content" android:layout_width="50dp"
android:textColor="#ffffff" android:textSize="22dp"
android:layout_marginLeft="5dp" android:layout_marginRight="5dp"
android:layout_marginTop="5dp" android:background="@drawable/do_nothing_button" android:layout_alignBottom="@+id/dotNumber"/>
<!-- Third row finish -->
</RelativeLayout>
Now, rightnow i have set the first row of Linearlayout as android:gravity="center_horizontal" which contain the button like "7","8","9" and "AC" button. But i want it to set autometicaly to fit with the device width. I think i have to use the weight property. but where and How i have to use it that i dont know So Please Help me regardin it. Thanks.
use the weightSum property in your layout tag and in your buttons tag also. Set it to 1 in your layout tag and divide it in the way you want to set your button and set this value to button like if you want 4 buttons to be displayed with equal proportion then 1/4=0.25, set 0.25 to each button tag.
Refer this and this.
Remove android:layout_centerInParent="true"
from LinearLayout
.
Set android:layout_width="fill_parent"
for LinearLayout
and Button
s.
Try setting android:layout_weight="1"
for every Button
.
First Set your LinearLayout
width as fill_parent
, and remove the gravity
, and also put android:layout_weight=1
for each of your children to share the equal width. set layout_width
parameter of each of the children to 0dp
精彩评论