The layout width is always zero in fragment examples. What is behind this value?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
To hopefully explain, take a look at the Design Philosophy for Fragments in the Dev Guide.
If you look at the image, to the left it shows how a phone would show an intial Activity A which would then start Activity B when an item in a list is selected.
To the right, however, it is showing how those two Activities can be shown as Fragments at the same time. Notice Fragment A is taking 1/3 of the screen and Fragment B is filling 2/3 of the screen.
Now look at the XML for that layout from Adding a fragment to an activity from the same Dev Guide article...
You can see that both Fragments have a
layout_width
of 0dp but they also each have alayout_weight
attribute. The first has a weight of 1 and the second a weight of 2.In short, when using a layout like this, setting the 'width' to be 0 means you don't want to explicitly enforce a width and that the OS should work out the relative widths as fractions of total weight. In other words 1+2=3 (total weight) but the first Activity wants a width of 1 / total weight = 1/3 of the screen width and Fragment B wants 2 / total width = 2/3 of the screen width.
Now suppose you add a third fragment which also has width=0dp and weight=2. In this case, the total weight is 1+2+2=5 and the first fragment will have a relative width of 1/5 and the other two fragments 2/5 of the screen or 20% / 40% / 40%.
This has worked for me:
Sum all weights in the layout. In the example that Squonk posted, there are 2 fragments and total weight is 3.
The fragment ArticleListFragment has a weight=1, meaning that the size will be 1/3 ( 3 is the total weight) of the screen.
The fragment ArticleReaderFragment has a weight =2, meaning that the size will be 2/3 of the screem.
Hope it helps.