Android自定义操作栏LinearLayout具有等间距按钮

时间:2015-01-06 17:26:12

标签: android android-actionbar android-linearlayout android-actionbar-compat

固定

我通过简单地删除操作栏并使用支持库的工具栏来解决此问题。我已将工具栏添加到我的activity的XML文件中。此外,我已经删除了我的根视图(RelativeLayout)的默认填充,这些填充在创建活动时已被Android Studio处理,并且最初未被我通知。此外,我使用“match_parent”而不是“0dp”作为图像按钮宽度。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BluetoothConnectionManager">

<android.support.v7.widget.Toolbar android:id="@+id/tbMenu"
    xmlns:application="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/hafnertec"
    android:layout_alignParentTop="true">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_marginRight="12dp"
        android:orientation="horizontal">

        <ImageButton android:id="@+id/miBluetoothConnection"
            android:src="@drawable/ic_action_bluetooth_searching"
            android:background="@drawable/selector_menu_button"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:scaleType="centerInside"
            android:onClick="showDialogueCloseBluetoothConnection"/>

        <ImageButton android:id="@+id/miHome"
            android:src="@drawable/ic_action_bluetooth_searching"
            android:background="@drawable/selector_menu_button"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:scaleType="centerInside"
            android:onClick="switchToHomeFragment"/>

        <ImageButton android:id="@+id/miGraphsBurnOffCurves"
            android:src="@drawable/ic_action_bluetooth_searching"
            android:background="@drawable/selector_menu_button"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:scaleType="centerInside"
            android:onClick="switchToBurnOffGraphsFragment"/>

        <ImageButton android:id="@+id/miConfigurationMode"
            android:src="@drawable/ic_action_bluetooth_searching"
            android:background="@drawable/selector_menu_button"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:scaleType="centerInside"/>

        <ImageButton android:id="@+id/miExpertMode"
            android:src="@drawable/ic_action_bluetooth_searching"
            android:background="@drawable/selector_menu_button"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:scaleType="centerInside"/>
    </LinearLayout>

</android.support.v7.widget.Toolbar>
...
</RelativeLayout>

原始问题

我正在编写一个使用自定义操作栏的Android应用程序。我希望此操作栏显示五个按钮,它们间隔相等。为完成此任务,我使用以下XML文件(由于图标仍处于工作状态,所有按钮共享相同的图标。):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_gravity="fill_horizontal"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:orientation="horizontal">

<ImageButton android:id="@+id/miBluetoothConnection"
    android:src="@drawable/ic_action_bluetooth_searching"
    android:background="@drawable/selector_menu_button"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2"
    android:scaleType="centerInside"
    android:onClick="showDialogueCloseBluetoothConnection"/>

<ImageButton android:id="@+id/miHome"
    android:src="@drawable/ic_action_bluetooth_searching"
    android:background="@drawable/selector_menu_button"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2"
    android:scaleType="centerInside"
    android:onClick="switchToHomeFragment"/>

<ImageButton android:id="@+id/miGraphsBurnOffCurves"
    android:src="@drawable/ic_action_bluetooth_searching"
    android:background="@drawable/selector_menu_button"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2"
    android:scaleType="centerInside"/>

<ImageButton android:id="@+id/miConfigurationMode"
    android:src="@drawable/ic_action_bluetooth_searching"
    android:background="@drawable/selector_menu_button"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2"
    android:scaleType="centerInside"/>

<ImageButton android:id="@+id/miExpertMode"
    android:src="@drawable/ic_action_bluetooth_searching"
    android:background="@drawable/selector_menu_button"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2"
    android:scaleType="centerInside"/>
</LinearLayout>

在设计师中工作得很好,在真实设备上测试它时不起作用。在测试时,所有按钮被挤压在一起,它们之间绝对没有间距。设计器中的预览和使用我的测试设备制作的屏幕截图如下所示。

Action bar displayed by the designer

Action bar on my testing device

我还尝试将LinearLayout包装到RelativeLayout以及使用TableLayout。但是,这些东西都不起作用。使用RelativeLayout,第五个按钮没有其他按钮那么大。

要将操作栏添加到我的应用程序,我使用以下代码:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    ...
    // add the custom action bar
    this.abMainMenu = this.getSupportActionBar();
    this.abMainMenu.setDisplayShowCustomEnabled(true);
    LayoutInflater liAddActionBar = LayoutInflater.from(this);
    this.customActionBar = liAddActionBar.inflate(ApplicationSettings.LAYOUT_REFERENCE_MAIN_MENU, null);
    this.abMainMenu.setCustomView(this.customActionBar);
    ...
}

我正在测试带有CyanogenMod 12月11日截图(Android 4.4.4)的三星Galaxy S I9000。

我感谢任何帮助!

谢谢,

的Lukas

1 个答案:

答案 0 :(得分:0)

您是否尝试使用工具栏?