在操作栏中向左添加一个按钮,在右侧添加另一个按钮

时间:2014-03-15 20:04:40

标签: android xml android-layout android-actionbar

我想在操作栏中添加两个按钮。一个到左侧,另一个到右侧。我的问题是,当我添加它们时,它们之间没有空间而且它们被拉伸了。 这是两个按钮的屏幕截图:enter image description here

这是单独放置的按钮,我希望两个按钮看起来像那样,但每个按钮都在一边: enter image description here

这是我使用的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:weightSum="2">

    <Button
        android:id="@+id/item1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/font_big"
        android:scaleType="centerInside" />

    <Button
        android:id="@+id/item2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/menu_button"
        android:scaleType="centerInside" />

</LinearLayout>

在第二个屏幕截图中,我没有使用操作栏的自定义布局。我希望按钮具有与使用操作栏的默认布局相同的小尺寸。 有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

从您的代码中,您将操作栏布局划分为两个相同加权的按钮,无论按钮大小如何,通常都会为每个按钮提供一半的布局大小。请检查

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

    <Button
        android:id="@+id/item1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/font_big"
        android:layout_alignParentLeft = "true"
        android:scaleType="centerInside" />

    <Button
        android:id="@+id/item2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/menu_button"
        android:layout_alignParentRight = "true"
        android:scaleType="centerInside" />

</RelativeLayout>