Android sets the width or height of the child control to Match_parent to fill the remaining width or height in the parent control

Source: Internet
Author: User

First a few, as follows:

The layout effect of the above four graphs is that if the parent control contains two child controls, the width of one of the child controls (in red button) is a fixed value, and the width of the other child control (in green button) is not fixed. To make the total width of the two child controls exactly equal to the width of the parent control. You can set the width of the control that is not fixed to match_parent, but some details need to be noted, or even if you set the match_parent, it does not appear as the effect. Note the following details:

    1. The above effects can be implemented using Relativelayout. Note that the horizontal position of the green button must be set relative to the red button, but not to the parent control. That is, the green button can only be set to android:layout_toleftof= "@id/red_button" or android:layout_torightof= "@id/red_button" and cannot be set to Android:layout_alignparentleft= "true" or android:layout_alignparentright= "true". Otherwise two buttons will overlap.
    2. The above effects, if implemented using LinearLayout, will have some limitations. LinearLayout can only be used if the width-determined control is on the left (or top), and the width-indeterminate control is on the right (or bottom). That is, using linearlayout can only achieve the effect of the first two graphs, but not the effect of the last two graphs (if used for the latter two effects, then the left button will occupy the width of the entire screen, and the button on the right will be squeezed out of the screen).

Attach the layout code:

1. Layout of Figure 1:

(1) Using relativelayout:

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" >    <ButtonAndroid:id= "@+id/red_button"Android:layout_width= "200DP"Android:layout_height= "Wrap_content"Android:layout_alignparentleft= "true"Android:background= "#ff0000"Android:text= "1" />    <ButtonAndroid:id= "@+id/green_button"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_torightof= "@id/red_button"Android:background= "#00ff00"Android:text= "222222" /></Relativelayout>

(2) Using LinearLayout:

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "Horizontal" >    <ButtonAndroid:id= "@+id/red_button"Android:layout_width= "200DP"Android:layout_height= "Wrap_content"Android:background= "#ff0000"Android:text= "1" />    <ButtonAndroid:id= "@+id/green_button"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:background= "#00ff00"Android:text= "222222" /></LinearLayout>

2. Layout of Figure 2:

(1) Using relativelayout:

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" >    <ButtonAndroid:id= "@+id/red_button"Android:layout_width= "50DP"Android:layout_height= "Wrap_content"Android:layout_alignparentleft= "true"Android:background= "#ff0000"Android:text= "1" />    <ButtonAndroid:id= "@+id/green_button"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_torightof= "@id/red_button"Android:background= "#00ff00"Android:text= "222222" /></Relativelayout>

(2) Using LinearLayout:

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "Horizontal" >    <ButtonAndroid:id= "@+id/red_button"Android:layout_width= "50DP"Android:layout_height= "Wrap_content"Android:background= "#ff0000"Android:text= "1" />    <ButtonAndroid:id= "@+id/green_button"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:background= "#00ff00"Android:text= "222222" /></LinearLayout>

3. Layout of Figure 3:

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" >    <ButtonAndroid:id= "@+id/red_button"Android:layout_width= "200DP"Android:layout_height= "Wrap_content"Android:layout_alignparentright= "true"Android:background= "#ff0000"Android:text= "1" />    <ButtonAndroid:id= "@+id/green_button"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_toleftof= "@id/red_button"Android:background= "#00ff00"Android:text= "222222" /></Relativelayout>

4. Layout of Figure 4:

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" >    <ButtonAndroid:id= "@+id/red_button"Android:layout_width= "50DP"Android:layout_height= "Wrap_content"Android:layout_alignparentright= "true"Android:background= "#ff0000"Android:text= "1" />    <ButtonAndroid:id= "@+id/green_button"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_toleftof= "@id/red_button"Android:background= "#00ff00"Android:text= "222222" /></Relativelayout>

Android sets the width or height of the child control to Match_parent to fill the remaining width or height in the parent control

Related Article

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.