【问题标题】:Android Custom Action Bar LinearLayout with equally spaced buttons具有等间距按钮的 Android 自定义操作栏 LinearLayout
【发布时间】:2015-01-07 18:48:29
【问题描述】:

已修复

我通过简单地移除操作栏并改用支持库的工具栏来解决此问题。我已将工具栏添加到我的活动的 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 上进行测试。

感谢您的帮助!

谢谢,

卢卡斯

【问题讨论】:

  • 我试图在我的布局中实现这个解决方案,但我得到了下一个问题:“引起:java.lang.ClassNotFoundException:找不到类'android.support.v7.widget.Toolbar ' on path: /data/app/com.adapta-2.apk at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)" ...我该如何解决这种情况?谢谢
  • 在我看来,您似乎忘记了包含 Android 支持库。我希望Google's tutorial 可以给你进一步的指示,包括图书馆。如果没有,请告诉我。

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


【解决方案1】:

您是否尝试过使用工具栏?

【讨论】:

  • 我遵循了这个指南:chris.banes.me/2014/10/17/appcompat-v21 但是,我仍然遇到同样的布局问题。
  • 您不需要在您的情况下调用 seSupportActionBar 方法。只需将工具栏视为任何其他布局。 ins styles.xml 选择一个没有操作栏的主题。
  • 另外,您没有在根布局中设置 weightSum 属性。并且每个按钮的宽度应该与父级匹配,以便权重起作用。
  • 我已成功添加包含我的菜单的操作栏。此外,我已经删除了 Android Studio 创建的默认边距,但我没有注意到。现在它完美无缺。为了平衡工具栏左侧较大的填充,我只是在右侧添加了一些 (12dp)。谢谢,Material Design ;) 感谢您的回答!
猜你喜欢
  • 2013-02-23
  • 1970-01-01
  • 2015-12-07
  • 2021-12-13
  • 2012-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多