如何在android中设置按钮的宽度以覆盖屏幕宽度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2354742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:31:11  来源:igfitidea点击:

how to set width of buttons in android to cover screen width?

androidlayout

提问by UMAR

friends,

朋友们,

i have following three button in linear layout with width fill_parent

我在线性布局中有以下三个按钮,宽度为 fill_parent

now how can i set width of these buttons to cover whole screen area equally?

现在如何设置这些按钮的宽度以平均覆盖整个屏幕区域?

any help would be appriciated.

任何帮助将被appriciated。

<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"
 />

回答by dxh

Give all buttons the following properties

为所有按钮提供以下属性

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

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

fill_parent告诉它们使用尽可能多的宽度,并weight确定当多个控件竞争同一空间时如何分配宽度。(尝试weight为每个按钮使用不同的值,看看它是如何工作的)

回答by Fedor

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>

回答by duggu

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.

你必须在每个按钮上做 0dp 的宽度。