android - 如何将一个按钮置于另外两个按钮之间?

标签 android

我正在尝试获取带有三个按钮的一行。最左边一张在中间,一张在最右边。理想情况下,我希望中心按钮占据两侧按钮之间的所有空间。现在,中心(菜单)按钮恰好与左侧(上一个)按钮重叠。这是我现在拥有的:

<RelativeLayout android:layout_marginTop="-50dip" android:gravity="bottom" android:layout_height="wrap_content" android:layout_width="fill_parent">
<Button
android:id="@+id/previous_button"
android:layout_height="wrap_content"
android:layout_width="110px"
android:layout_alignParentLeft="true" 
android:text="@string/previous"
/> 

<Button
android:id="@+id/menu"
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:scaleType="center" 
android:text="@string/menu"
/> 

<Button
android:id="@+id/next_button"
android:layout_height="wrap_content"
android:layout_width="110px"
android:layout_alignParentRight="true"
android:text="@string/next"
/>        

最佳答案

您应该避免使用像素值作为尺寸。如果您确实想设置明确的值,请使用 dpi 单位。

在这种情况下,您应该能够使用简单的 LinearLayout layout_weights 。 layout_weight 指示如何在小部件之间分配剩余空间。如果您将一个小部件的值设置为 1,而将其他小部件的值设置为 0,则设置为 1 的小部件将耗尽所有额外空间。

<LinearLayout 
  android:layout_height="wrap_content"
  android:layout_width="fill_parent">
  <Button 
    android:id="@+id/previous_button"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_weight="0"/>
  <Button  
    android:id="@+id/menu"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
   android:layout_weight="1"/>
  <Button  
    android:id="@+id/next_button" 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_weight="0"/>
</LinearLayout>

关于android - 如何将一个按钮置于另外两个按钮之间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3621239/

相关文章:

java - 添加操作按钮 : action_search cannot be resolved or is not a field

Java静态内部类返回null

android从java设置默认首选项

android - 数据更改上的 firebase 在附加值事件监听器中被多次调用

java - 扩展类必须与其扩展的类具有相同的访问级别

android - 更改 Android 版 Facebook SDK 的语言

java - 通过属性读取器设置 TextView 的文本

java - getActivity().findViewById(int id) 即使在 fragment 完成构建后也会返回 null

Chrome 上使用 ARC 的 Android 应用程序,启用后退按钮在顶部

android - 如何使用新更新的 Google Play 游戏玩家 ID 检索 Google Play 游戏 ID?