Android uses tablelayout layout stretch width

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/ghj1976/archive/2011/04/21/2023850.html

Layout file

<?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 " ><tablelayout android:layout_width= "match_parent" android:layout_height= "Match_parent" ><TableRow> <edittext android:id= "@+id/et_led" android:layout_width= "fill_parent" android:digits= "1234567890.+-*/=%\n\t ()" android:text= "" android:layout_span= "4"/></tablerow><tablerow><button android:text= "(" Android: Id= "@+id/btn_leftparenthesis" ></button><button android:text= ")" Android:id= "@+id/btn_ Rightparenthesis "></button><button android:text=" * "android:id=" @+id/btn_x "></Button>< Button android:text= "Back" android:id= "@+id/btn_back" ></Button></TableRow><TableRow>< Button android:text= "7" android:id= "@+id/btn_7" ></button><button android:text= "8" android:id= "@+id/btn _8 "></buTton><button android:text= "9" android:id= "@+id/btn_9" ></button><button android:text= "%" Android: Id= "@+id/btn_mod" ></button></tablerow><tablerow><button android:text= "4" android:id= "@+ Id/btn_4 "></button><button android:text=" 5 "android:id=" @+id/btn_5 "></button><button android:text= "6" android:id= "@+id/btn_6" ></button><button android:text= "/" android:id= "@+id/btn_div" ></button></tablerow><tablerow><button android:text= "1" android:id= "@+id/btn_1" ></ Button><button android:text= "2" android:id= "@+id/btn_2" ></button><button android:text= "3" Android:id= "@+id/btn_3" ></button><button android:text= "-" android:id= "@+id/btn_sub" ></Button ></tablerow><tablerow><button android:text= "0" android:id= "@+id/btn_0" ></Button>< Button android:text= "." Android:id= "@+id/btn_dot" ></button><button android:text= "+" android:id= "@+id/Btn_plus "></button><button android:text=" = "android:id=" @+id/btn_equal "></Button></ Tablerow></tablelayout></linearlayout>

Layout effect:

Obviously this does not meet our expectations, below we demonstrate the use of android:stretchcolumns from the dynamic stretching

Here we simply add a property to Tablelayout android:stretchcolumns= "*" means that all columns are automatically stretched to fit the screen width.

Layout effect

Its value can be a number, or it can be *, note that the number is starting from 0, that is: android:stretchcolumns= "1" is the second column that sets tablelayout all rows.

We'll see that the buttons on the fourth column are wider than the buttons on the other columns, so what if we want to be as wide?

An easy way:

On the basis of automatic stretching, the width of each column is set to the same, such as the following code:

<?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 " ><tablelayout android:layout_width= "match_parent" android:layout_height= "Match_parent" Android: stretchcolumns= "*" ><tablerow><edittext android:id= "@+id/et_led" android:layout_width= "Fill_parent" Android:digits= "1234567890.+-*/=%\n\t ()" android:text= "" android:layout_span= "4"/></tablerow>< Tablerow><button android:text= "(" Android:id= "@+id/btn_leftparenthesis"android:layout_width= "1dip"></button><button android:text= ")" android:id= "@+id/btn_rightparenthesis" android:layout_width= "1dip" ></button><button android:text= "*" android:id= "@+id/btn_x" android:layout_width= "1dip" ></Button ><button android:text= "Back" android:id= "@+id/btn_back" android:layout_width= "1dip" ></button></ Tablerow><tablerow><button android:text= "7" android:id= "@+id/btn_7" ></button><button android:text= "8" android:id= "@+id/btn_8" ></button><button android:text= "9" android:id= "@+id/btn_9" ></button><button android:text= "%" android:id= "@+id/btn_mod" ></Button></TableRow>< Tablerow><button android:text= "4" android:id= "@+id/btn_4" ></button><button android:text= "5" Android:id= "@+id/btn_5" ></button><button android:text= "6" android:id= "@+id/btn_6" ></Button> <button android:text= "/" android:id= "@+id/btn_div" ></button></tablerow><tablerow><buTton android:text= "1" android:id= "@+id/btn_1" ></button><button android:text= "2" android:id= "@+id/btn_2 "></button><button android:text=" 3 "android:id=" @+id/btn_3 "></button><button android:text = "-" android:id= "@+id/btn_sub" ></button></tablerow><tablerow><button android:text= "0" Android:id= "@+id/btn_0" ></button><button android:text= "." Android:id= "@+id/btn_dot" ></Button ><button android:text= "+" android:id= "@+id/btn_plus" ></button><button android:text= "=" Android: Id= "@+id/btn_equal" ></Button></TableRow></TableLayout></LinearLayout>

The effect is as follows:

Note that there are 2 more settings than the original one.

android:layout_width= "1dip " and android:stretchcolumns= "*"

Android uses tablelayout layout stretch width

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.