如何在 Android 中创建两个视图,每个视图使用 50% 的高度,除非其中一个较小?
想象一个完整的 Android 设备屏幕,我希望它分为两部分:
- 上半部分有文本,它可能大于可用空间(或没有),因此文本将滚动(即 ScrollView 内的 TextView
- )下半部分包含一个 MapView 控件。
具体看一些场景:
- 如果文字很小,我希望地图占用更多的空间,即超过50%。所以也许 20% 是文字,80% 是地图。
- 如果文本较大,则最多仅占用屏幕空间的 50%,然后滚动。所以 50% 是地图,50% 是文字。
目前,我已经为这两个部分分配了权重,这还不错,但如果文本很小,地图就不会扩展以占据空间,并且布局会浪费地图可能会出现的间隙有用地使用。
我已经尝试了很多组合,但不知道如何实现这一点。我知道自己想要什么,但不知道如何获得可用的视图来交付它,这对我来说似乎是一种常见的经历。我希望有一种简单的方法可以做到这一点。
请随意让我看起来像个傻瓜,并指出我错过的明显属性:-)
============================= =============================================
据我所知无法仅在声明性 XML 中执行此操作,需要在代码中执行。我将文本部分的高度设置为wrap_content,权重设置为0(不调整大小),并将地图设置为权重=1(即占用剩余空间)。然后,我检查文本部分(在 ScrollView 中)是否占用了太多空间,如果是,则将其缩小。需要更改此代码以支持不同的布局方向。
private void fixLayoutProportions()
{
float maxPercentageOfScreenForText = 50/100;
LinearLayout container = (LinearLayout)findViewById(R.id.container);
ScrollView eventText = (ScrollView)findViewById(R.id.text_scroller);
int heightAvailable = container.getHeight();
int scrollerHeight = eventText.getHeight();
if ( scrollerHeight>(heightAvailable*maxPercentageOfScreenForText) ) // Text section using too much space
{
eventText.getLayoutParams().height = (int)(heightAvailable*maxPercentageOfScreenForText) ;
eventText.invalidate();
}
}
Imagine a full Android device screen, I want it split in to two sections:
- The upper half has text in it, which may be larger than the space available (or not) and so the text will scroll (i.e. TextView inside a ScrollView)
- The lower half contains a MapView control.
Looking specifically at some scenarios:
- If the text is small, I want the map to take up more space, i.e. more than 50%. So perhaps 20% text, 80% map.
- If the text is larger, it only takes up a MAXIMUM of 50% of the screen space, and then scrolls. So 50% map, 50% text.
At the moment I've assigned weights to the two parts, and that isn't too bad, but if the text is small, the map doesn't expand to take the space, and the layout has a wasted gap that the map could usefully use.
I've tried loads of combinations but can't see how to make this happen. It seems to be a common experience for me that I know what I want, but can't see how to get the available views to deliver it. I'm hoping there's a nice easy way to do this.
Please feel free to make me look like a fool and point out the obvious attribute I've missed :-)
======================================================================
As far as I can see there's no way to do this just in declarative XML and it needs doing in the code. I set the text section height to wrap_content, weight to 0 (no resizing), and have the map set to weight=1 (i.e. take up the remaining space). I then check if the text section (in a ScrollView) is taking up too much space and if so, shrink it back. This code would need changing to support a different layout orientation.
private void fixLayoutProportions()
{
float maxPercentageOfScreenForText = 50/100;
LinearLayout container = (LinearLayout)findViewById(R.id.container);
ScrollView eventText = (ScrollView)findViewById(R.id.text_scroller);
int heightAvailable = container.getHeight();
int scrollerHeight = eventText.getHeight();
if ( scrollerHeight>(heightAvailable*maxPercentageOfScreenForText) ) // Text section using too much space
{
eventText.getLayoutParams().height = (int)(heightAvailable*maxPercentageOfScreenForText) ;
eventText.invalidate();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以通过将所有内容放入 LinearLayout 并更改以下参数来实现:
You can do it by putting everything into LinearLayout and changing following parameters:
您是否尝试在运行时测量屏幕高度:
然后,将顶视图 max_height 设置为 width*0.5,将 min_height 设置为 width*0.2。您的顶视图必须是具有 min_height 和 max_height 属性的控件(如 TextView)。另外,将layout_weight 设置为0 或留空。
在底部视图中将布局权重设置为 1。
Did you try to measure your screen hight at run time:
Then, set your top view max_height to width*0.5 and min_height to width*0.2. Your top view has to be control (like TextView) that has min_height and max_height properties. Also, set layout_weight to 0 or leave it empty.
On your bottom view set layout weight to 1.
在 XML 中实现 50/50 的最简单方法是使用 LinearLayout 权重。基本上将两个视图放入一个 LinearLayout 中,将两个子视图上的 android:layout_weight 设置为相同的值,例如将两者设置为 0.5、1 或 42。然后将 layout_width 设置为 0px 或 fill_parent/match_parent。
较小的部分变得更加复杂。幸运的是,您可以在 Java 中关闭称重。一种方法是等到窗口被绘制(如果它们是预先填充的)并测量它们。这可以在我认为它被称为 onWindowFocusChanged 上完成
The easiest way to do 50/50 is in XML is using LinearLayout weights. Basically put the two views into a single LinearLayout set the android:layout_weight on both child views to the same value, like setting both to .5, 1, or 42. You then set the layout_width to 0px or fill_parent/match_parent.
The smaller part gets more complicated. Luckily, you can turn off weighing in Java. One way is to wait until the windows get drawn (if they are pre-populated) and measure them. This can be done on I think it was called onWindowFocusChanged
我还没有对此进行测试,但我会尝试将您的 ScrollView 设置为具有
android:layout_weight="1"
并将您的MapView
设置为具有android:layout_weight= “0”
和android:minHeight =“240dp”
。希望 minHeight 优先于layout_weight。I haven't tested this, but I'd try setting your ScrollView to have
android:layout_weight="1"
and yourMapView
to haveandroid:layout_weight="0"
and aandroid:minHeight="240dp"
. The hope is that minHeight will have precedence over layout_weight.我认为你必须将mapviews高度设置为填充父级并将textviews高度设置为换行内容,并且对于滚动,你必须将文本视图的垂直滚动设置为true,并且当你需要不超过50%的空间textview时,你可以设置textview的maxheight属性。
i think you have to set mapviews height as fill parent and set textviews height as wrap contetn and than for scrolling you have toset vertical scrool true for text view and as you nedded not more than 50% space textview you can set maxheight property of textview.
如果您觉得这很琐碎,我很抱歉。
但我建议你这样做,以防它没有打动你。
如何使用相对布局并使文本视图始终位于地图顶部。
使textview的重力为top,宽度为match_parent,高度为wrap_content,权重为1(与地图相同)。这样你的文本视图就会根据文本的大小而改变,同时不会因为重量而超过 50%。至于地图,用户可以向下拉地图来查看textview下隐藏的部分。就好像文本视图下没有地图一样(你知道,除非你想让文本视图背景透明,我认为这看起来很酷:))。我不知道地图视图。但我假设它会像 iPhone 上的谷歌地图一样,就像你可以使用多点触控来改变大小并使用单点触控来滚动一样。
I am sorry if you find this trivial.
But I am suggesting it in case it did not strike you.
How about using relative layout and keeping the textview always on top of the map.
Make the gravity of the textview top, its width match_parent, its height wrap_content and its weight 1(same as that of thee map). That way your textview will change according to the size of the text, while not going above 50% because of the weight. As for the map, the user can pull the map down to see the hidden part under textview. It'll be as if there is no map under the textview(you know, unless you want to make the textview background transparent which i think would look cool :) ). I do not know about the map view. But I am assuming it'll be something like google maps on iphone, like you can vary size using multi-touch and scroll using single.
好吧,我看到它就像您将屏幕的最大一半保留给 TextView,如果更多,则必须滚动。我有一个解决方案,但为此你必须修复 TextView 的最大行数,计算这可能会很痛苦:P 但无论如何都有一个解决方案。
main.xml 文件
Java 文件:
如果文本仅包含 2 行“Does it work?”会自动升档。但我在这里看到的唯一问题是最大行数为 5,您可以为 ui 设定最大值,猜测这可能会很好用:)
BR,
J
P.S.我之前没有回答很多问题,所以我不确定如何附加文件:(
OK as I see its something like u are reserving at max half of ur screen to the TextView and if its more it has to scroll. I have a solution but for that you will have to fix the max no.l of lines for TextView, calculating which can be a pain :P but have a solution never the less.
main.xml file
Java file:
If the text is say only in 2 lines "Does it work?" will shift up automatically. But the only problem i see here is the max lines to 5 is you can an max value for u i guess this might work well :)
BR,
J
P.S. I haven't answered many questions before so i m not sure how to attach files :(