Android linearlayout space between items

Android layout with equal spacing between elements [closed]

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 7 years ago .

I am trying to make a nice set of items, which are equally spaced from each other. On example below, I set buttons between empty textview. Every button and text view has height = 0dip and weight=1 . And it looks pretty nice. However, is there a way to clean up the layout file?

1 Answer 1

I don’t know what the standards are for Layout Naming on Android, but I can tell you that these names are not very good.

  • textView2
  • btn_distance_walked
  • textView3

none of these accurately describes what they do.

you should probably look for naming schemes on Google.

btn_distance_walked doesn’t tell us what the button is going to do. What is it going to do with the distance walked? It should be something like calculate_Distance or calculateDistance (my preferred naming scheme camelCasing)

Update:

I recently answered a question similar to this one and came up with a name scheme for buttons, action_ObjectActedUpon this may be more specific to the naming issue in your code as well.

Your indentation seems to be off as well, it should look like this

I just answered a similar question and I think that I should add a little bit of that answer to this one..

Another way that you can reduce clutter in the layout is to define some styles by creating an xml file in the res/values/ folder of your project and do this

and then in your Layout XML you add the style to the layout element like this

Источник

Is it possible to evenly distribute buttons across the width of a LinearLayout

I have a LinearLayout (oriented horizontally) that contains 3 buttons. I want the 3 buttons to have a fixed width and be evenly distributed across the width of the LinearLayout .

I can manage this by setting the gravity of the LinearLayout to center and then adjusting the padding of the buttons, but this works for a fixed width and won’t work for different devices or orientations.

23 Answers 23

Expanding on fedj’s answer, if you set layout_width to 0dp and set the layout_weight for each of the buttons to 1, the available width will be shared equally between the buttons.

If you don’t want the buttons to scale, but adjust the spacing between the buttons (equal spacing between all buttons), you can use views with weight=»1″ which will fill the space between the buttons:

Читайте также:  Slender man arrival для андроид

You can do this by giving both View s a layout_width of 0dp and a layout_weight of 1 :

The way android layout_weight works is that:

  • first, it looks to the size that a View would normally take and reserves this space.
  • second, if the layout is match_parent then it will divide the space that is left in the ratio of the layout_weight s. Thus if you gave the Views layout_weight=»2″ and layout_weight=»1″ ,the resultant ratio will be 2 to 1,that is : the first View will get 2/3 of the space that is left and the other view 1/3.

So that’s why if you give layout_width a size of 0dp the first step has no added meaning since both Views are not assigned any space. Then only the second point decides the space each View gets, thus giving the View s the space you specified according to the ratio!

To explain why 0dp causes the space to devide equally by providing an example that shows the opposite: The code below would result in something different since example text now has a width that is greater than 0dp because it has wrap_content instead making the free space left to divide less than 100% because the text takes space. The result will be that they do get 50% of the free space left but the text already took some space so the TextView will have well over 50% of the total space.

Источник

Evenly spacing views using ConstraintLayout

A common use for LinearLayout is to evenly space (weight) views, for example:

How do you implement evenly spaced views like this using the new ConstraintLayout ?

ConstraintLayout links for reference: blog post, I/O session video

5 Answers 5

There are two ways to accomplish this using ConstraintLayout : Chains and Guidelines. To use Chains, make sure you are using ConstraintLayout Beta 3 or newer and if you want to use the visual layout editor in Android Studio, make sure you are using Android Studio 2.3 Beta 1 or newer.

Method 1 — Using Chains

Open the layout editor and add your widgets as normal, adding parent constraints as needed. In this case, I have added two buttons with constraints to the bottom of the parent and side of the parent (left side for Save button and right side for Share button):

Note that in this state, if I flip to landscape view, the views do not fill the parent but are anchored to the corners:

Highlight both views, either by Ctrl/Cmd clicking or by dragging a box around the views:

Then right-click on the views and choose «Center Horizontally»:

This sets up a bi-directional connection between the views (which is how a Chain is defined). By default the chain style is «spread», which is applied even when no XML attribute is included. Sticking with this chain style but setting the width of our views to 0dp lets the views fill the available space, spreading evenly across the parent:

Читайте также:  Андроид как сохранять файлы

This is more noticeable in landscape view:

If you prefer to skip the layout editor, the resulting XML will look like:

  • setting the width of each item to 0dp or MATCH_CONSTRAINT lets the views fill the parent (optional)
  • the views must be linked together bidirectionally (right of save button links to share button, left of share button links to save button), this will happen automatically via the layout editor when choosing «Center Horizontally»
  • the first view in the chain can specify the chain style via layout_constraintHorizontal_chainStyle , see the documentation for various chain styles, if the chain style is omitted, the default is «spread»
  • the weighting of the chain can be adjusted via layout_constraintHorizontal_weight
  • this example is for a horizontal chain, there are corresponding attributes for vertical chains

Method 2 — Using a Guideline

Open your layout in the editor and click the guideline button:

Then select «Add Vertical Guideline»:

A new guideline will appear, that by default, will likely be anchored to the left in relative values (denoted by left-facing arrow):

Click the left-facing arrow to switch it to a percentage value, then drag the guideline to the 50% mark:

The guideline can now be used as an anchor for other views. In my example, I attached the right of the save button and the left of the share button to the guideline:

If you want the views to fill up the available space then the constraint should be set to «Any Size» (the squiggly lines running horizontally):

(This is the same as setting the layout_width to 0dp ).

A guideline can also be created in XML quite easily rather than using the layout editor:

Источник

Android — LinearLayout Horizontal with wrapping children

Is there a property to set for Android’s LinearLayout that will enable it to properly wrap child controls?

Meaning — I have changeable number of children and would like to lay out them horizontally like:

I do that by setting:

However, if I have large number of children, last one gets cuts off, instead of going to next line.

Any idea how this can be fixed?

If you want something like this.

12 Answers 12

As of May 2016 Google has created its own FlexBoxLayout which should solve your problem.

This should be what you want:

and the XML file

For anyone who needs this kind of behaviour:

Old question, but in case someone ends up here, two libraries that do exactly that:

Читайте также:  Android google play rate

Looking for solution for similar but simpler problem, that is to wrap child text content in horizontal layout. kape123’s solution works fine. But find a simpler one for this problem, using ClickableSpan. Maybe it could be useful for some simple case. snippet:

In the past many custom solutions and libraries tried and indeed solved this problem.

Starting with Constraint Layout 2.0 we can now use Flow

Flow is a new virtual layout for building chains that can wrap to the next line, or even another section of the screen, when they run out of room. This is useful when you’re laying out multiple items in a chain but you’re not quite sure how big the container will be at runtime. You can use this to build your layout based on a dynamic size in your application, like screen width on rotation.

Here is how the xml will look like:

Notice app:constraint_referenced_ids and app:flow_wrapMode properties.

We pass the views using the first one and we choose how to wrap them with the second.

app:flow_wrapMode accepts 3 different options:

none: create a single chain, overflowing if the content doesn’t fit

chain: on overflow, create add another chain for the overflow elements

align: similar to chain, but align rows into columns

Источник

Is it advisable to use LinearLayout inside ConstraintLayout in Android?

I am new to ConstraintLayout in Android and newbie to Android too. I have a question. Is it advisable to use LinearLayout inside ConstraintLayout ? For example:

Also curious to know how popular is the ConstraintLayout among the developers?

2 Answers 2

Is it advisable to use LinearLayout inside ConstraintLayout?

In general, the idea behind ConstraintLayout is that it allows you to position all of your children without having to nest any other ViewGroup s inside the ConstraintLayout . As such, I would say that it is not advisable.

However, there are some things that a LinearLayout can do that a ConstraintLayout can’t (mostly revolving around weighted spacing of views), and so if you need these particular corner cases in your layout, you won’t have any option other than falling back to a LinearLayout .

how popular is the ConstraintLayout among the developers?

ConstraintLayout is relatively new, but it is quite powerful and certainly something that you ought to familiarize yourself with. It won’t always be the perfect tool for the job at hand, but it will often allow you to easily create layouts you would otherwise spend hours on.

I can’t speak to widespread adoption statistics, but I can say that I’ve seen tons of questions on this site about the correct usage of ConstraintLayout , so clearly devs around the world are starting to work with it.

Источник

Оцените статью