开发者

How to make buttons cover full screen width in Android Linear Layout?

开发者 https://www.devze.com 2022-12-21 18:36 出处:网络
I have following three button in a Linear Layout with width fill_parent. How can I set the width of these buttons to equally cover the whole screen area?

I have following three button in a Linear Layout with width fill_parent.

How can I set the width of these buttons to equally cover the whole screen area?

<Button 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:id="@+id/btnReplyMessage" 
   android:gravity="left"
   android:text="Reply" 
/>
    
<Button 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:id="@+id/btnMarkAsUnread"
   android:gravity="left" 
   android:text="Mark as unread" 
/>
            
<ImageButton 
   android:id="@+id/btnDeleteMessage"
   android:src="@drawable/imgsearch"
   android:gravity="right" 
   android:layout_height="wrap_content"
   android:layout_width="wrap_content"
   android:layout_alignParentRight=开发者_高级运维"true"
 />


Give all buttons the following properties

android:layout_width="fill_parent"
android:layout_weight="1"

fill_parent tells them to consume as much width as possible, and weight determines how that width shall be distributed, when more than one control are competing for the same space. (Try playing around with different values of weight for each button to see how that works)


You should just specify these attributes for each button:

android:layout_width="fill_parent"
android:layout_weight="1"

So it should be something like that:

<LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">
  <Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
  <Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
  <Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
</LinearLayout>


you can use following code:

  <Button
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_weight="1"/>

You must to do 0dp in width on every button.

0

精彩评论

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