开发者

problem with 2 button layout in android

I have two buttons (buy and share) in my layout and I want them to occupy equal space. However, "share" button is occupying much more space than "buy" button. Can any one kindly point out my mistake ? Thanks.

            <B开发者_开发百科utton
            android:id="@+id/shareButton"
            android:text="Share"
            android:background="@drawable/ebutton"
            android:layout_width="wrap_content"
            android:layout_height="50px"
            android:paddingTop="10dip"
            android:paddingBottom="10dip"
            android:paddingLeft="10dip"
            android:paddingRight="10dip"
            android:layout_alignParentBottom="true"
            >
            </Button>

            <Button
            android:id="@+id/buyButton"
            android:layout_toRightOf="@+id/shareButton"
            android:text="Buy"
            android:background="@drawable/ebutton"
            android:layout_width="wrap_content"
            android:layout_height="50px"
            android:paddingTop="10dip"
            android:paddingBottom="10dip"
            android:paddingLeft="10dip"
            android:paddingRight="10dip"
            android:layout_alignParentBottom="true"
            >
            </Button>


Put both buttons in a LinearLayout and define a weightSum of 2 for the LinearLayout, then set the weight of each button to 1 and the two views will be scaled to occupy half of the parent.

<LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="2">  
        <Button android:id="@+id/buyBtn" 
            android:layout_weight="1"
            android:layout_width="0dp" 
            android:layout_height="fill_parent"
            android:text="Buy"/>

        <Button android:id="@+id/shareBtn" 
            android:layout_weight="1"
            android:layout_width="0dp" 
            android:layout_height="fill_parent"
            android:text="Share"/>
</LinearLayout>

This is a really powerful layout technique as you can define a what fraction of a parent a view should occupy. For example if you wanted 3 buttons with the first occupying half the screen and the other two occupying a quarter you could set the parent weightSum to 4, and the weight of the first to 2 and the weight of the other two buttons to 1.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜