What is the ButtonBarLayout and how should we use it?

后端 未结 7 1763
一向
一向 2021-02-03 19:57

When I developed, I found a new widget called android.support.v7.widget.ButtonBarLayout unexpectedly. I tried to search it on the internet, but nothing was found, e

相关标签:
7条回答
  • 2021-02-03 20:03

    The source code describes ButtonBarLayout as follows:

    /**
     * An extension of LinearLayout that automatically switches to vertical
     * orientation when it can't fit its child views horizontally.
     */
    

    So, in essence, it is nothing but a smart LinearLayout which manages auto-switching orientations based on available space on screen.

    The same ButtonBarLayout.java file describes mAllowStacking in comments as follows:

    /** Whether the current configuration allows stacking. */
    

    Source Code Here

    0 讨论(0)
  • 2021-02-03 20:05

    As others pointed out, the class description tells exactly what it is: an extension of LinearLayout that automatically switches to vertical orientation when it can't fit its child views horizontally.

    I might add that this was clearly done to fit with the material design specifications about dialogs. They make a distinction between side by side buttons and stacked buttons. See for example:

    Side-by-side buttons are recommended when the text of each label does not exceed the maximum button width, such as the commonly used OK/Cancel buttons.

    While you should go for stacked buttons when the single button is too large, or there's not enough room for both:

    When text labels exceed the maximum button width, use stacked buttons to accommodate the text. Affirmative actions are stacked above dismissive actions.

    So, one possible use of this class, is when designing your own dialogs. For example, AlertDialog and AlertDialog.Builder offer internal support for dialogs with buttons, but sometimes you just want to subclass DialogFragment or AppCompatDialogFragment for a better control.

    There, it might be useful to setup a bottom button bar that follows the design guidelines, and have full control on the buttons (like enabling and disabling, things you can't do with an AlertDialog AFAIK).

    0 讨论(0)
  • 2021-02-03 20:06

    Just to add to the other answers, if you guys want to check the orientation of a ButtonBarLayout you should check the orienation AFTER the value has called on measure.

    In other words (Kotlin):

    buttonBarLayout.post {
        val orientation = buttonBarLayout.orientation
        val height = buttonBarLayout.measuredHeight
    }
    
    0 讨论(0)
  • 2021-02-03 20:10

    https://android.googlesource.com/platform/frameworks/base/+/master/core/java/com/android/internal/widget/ButtonBarLayout.java

    Looking into the code, I think it's a LinearLayout for buttons (duh). You can probably look at it like the Dialog buttons divided by a vertical spacer: | . AllowStacking will change the orientation to vertical and the gravity to the right instead of bottom. I should try it out to give a better answer

    0 讨论(0)
  • 2021-02-03 20:17

    Regarding your question:
    How should we use it?

    I guess it is undocumented because it is not stable yet.
    It just popped up because this long lasting complaint originate from poor ROM modification by device vendor.
    https://code.google.com/p/android/issues/detail?id=78377

    See #270 for the resolution regarding classpath and why all classes inside .internal. were made public. And nope even that fix a lot of bugs from poor ROM modification are still out there (in lots of device of well known brands). The issue is soon declined by project member.

    I don't think we should use it just yet until the document show up.
    Just my $.02 though.

    0 讨论(0)
  • 2021-02-03 20:18

    ButtonBarlayout is not featured anywhere in the official Android documentation. it is used for auto-switching orientations according to the space.

    0 讨论(0)
提交回复
热议问题