如何在Android的两个视图使用50%的高度每次,除非是小?视图、高度、两个、如何在

2023-09-05 05:31:06 作者:你比纸巾还能扯

想象一下,一个完整的Andr​​oid设备的屏幕,我希望它分为两个部分:

在上半部有文字,这可能比用(或没有)的空间较大,所以文本将滚动(即内部的滚动型的TextView) 在下半部分包含一个图形页面的控制。

具体看,在某些情况下:

如果文字太小,我希望地图占用更多的空间,即超过50%。因此,也许20%的文字,80%的地图。 如果文本是较大的,只需要一个最大的屏幕面积的50%,然后滚动。因此,50%的地图,50%的文字。

目前,我已经分配的权重为两部分,那是不是太糟糕,但如果文字是小,地图不扩大到占空间,而且布局有一个浪费的差距在地图上可以有效使用。

我试过的组合负荷,但不能看到如何做到这一点。这似乎是对我来说是共同的经验,我知道我想要什么,但看不到如何让现有视图提供它。我希望有一个不错的简单的方法来做到这一点。

请随时让我看起来像个傻瓜,并指出了明显的属性,我已经错过了: - )

=============================================== =======================

据我可以看到有没有办法做到这一点只在声明XML,它需要做的code。我将文字部分高度WRAP_CONTENT,权重为0(未调整大小),并有地图设定为权重= 1(即占用的剩余空间)。然后我检查文本部分(在滚动型)占用了太多的空间,如果是这样,缩了回去。这code将需要改变,以支持不同的布局方向。

 私人无效fixLayoutProportions()
{
    浮maxPercentageOfScreenForText = 50/100;
    的LinearLayout容器=(的LinearLayout)findViewById(R.id.container);
    滚动型eventText =(滚动型)findViewById(R.id.text_scroller);
    INT heightAvailable = container.getHeight();
    INT scrollerHeight = eventText.getHeight();
    如果(scrollerHeight>(heightAvailable * maxPercentageOfScreenForText))//文字部分占用了过多的空间
    {
        。eventText.getLayoutParams()身高=(INT)(hei​​ghtAvailable * maxPercentageOfScreenForText);
        eventText.invalidate();
    }
}
 

解决方案

你尝试来衡量你的屏幕HIGHT在运行时:

 显示显示=((窗口管理器)
      getSystemService(Context.WINDOW_SERVICE))getDefaultDisplay()。
INT宽度= display.getHeight();
 
如何在Android的自定义列表视图中使用搜索功能

然后,设置您的俯视图max_height到宽* 0.5和min_height到宽* 0.2。你顶视图必须控制(如TextView中)已min_height和max_height性能。此外,layout_weight设置为0或保留空白。 在你的底视图设置布局权重为1。

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();
    }
}

解决方案

Did you try to measure your screen hight at run time:

Display display = ((WindowManager) 
      getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
int width = display.getHeight();

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.