gpt4 book ai didi

android - 窗口中可用空间的尺寸(不包括操作栏和边距)

转载 作者:行者123 更新时间:2023-11-29 00:23:10 25 4
gpt4 key购买 nike

免责声明:我重写了这个问题的大部分内容,包括它的标题,因为我已经部分弄明白了。


我想在所有屏幕尺寸上最大化按钮的大小,因为当它太小时看起来很傻。它看起来应该类似于:

(遗憾的是,我不能包含图片,因为我的声誉太低了。)

但如果我转动设备的方向,例如,按钮会匹配它的父级宽度,变得不协调。

(遗憾的是,我不能包含图片,因为我的声誉太低了。)

我现在已经弄清楚如何获取其父项(LinearLayout)的尺寸以及如何设置按钮的大小。我使用了以下代码:

  • window 是包含(仅)按钮的 LinearLayout 的 ID。
  • 此代码位于 MainActivity 的 onCreate() 方法中。

    // Adapt button's size to smaller dimension:
    final View window = findViewById(R.id.window);
    window.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
    int width = window.getMeasuredWidth();
    int height = window.getMeasuredHeight();
    int smallerSize;

    if (width < height) {
    smallerSize = width;
    } else {
    smallerSize = height;
    }

    View button = findViewById(R.id.fartButton);
    button.setLayoutParams(new LinearLayout.LayoutParams(smallerSize, smallerSize));

    window.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    }
    });

这种方法的问题是,它似乎没有考虑填充。按钮在较小的一侧被切掉了一点(在纵向模式下它的宽度,在横向模式下它的高度)。
有趣的是,按钮内的图像与窗口完美契合。例如,如果高度被切断了一点,图像仍然可以在其整个高度上看到(只有按钮的一些“额外”部分被切断,比如小边框和阴影)。

有没有办法获得按钮的最大尺寸,即窗口的尺寸,但没有操作栏和减号填充,以防止按钮的任何部分被切断?

最佳答案

您上面的示例“看起来应该类似于:”似乎没有加载,插图会有所帮助...

但是你可以使用 android:layout_weight 很好地管理屏幕比例

我不确定我是否预想了您的确切需求,但您可以尝试这样的操作:

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="X"
android:text=" "
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="button"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="X"
android:text=" "
/>
</LinearLayout>

X 的不同值将控制 View 中按钮的水平纵横比。

关于android - 窗口中可用空间的尺寸(不包括操作栏和边距),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21650761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com