Dimensions of usable space in window (action bar and margin excluded)

我怕爱的太早我们不能终老 提交于 2019-12-06 12:11:29

Your example above "should look similar to this:" does not seem to have loaded, illustration would help...

But you can manage screen proportions pretty well using android:layout_weight

I'm not sure I'm envisioning your exact needs, but you might try something like this:

<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>

where different values for X would control the horizontal aspect ratio for your button in a view.

I just figured it out. Was much easier than I thought. Thanks to everyone who answered, though. It helped me a lot on the way!


The padding that is applied to the window can easily be accesed through the getPadding...() methods. I just needed to adjust the part where the width and height get saved:

int width = window.getMeasuredWidth() - window.getPaddingLeft() - window.getPaddingRight();
int height = window.getMeasuredHeight() - window.getPaddingTop() - window.getPaddingBottom();

I thought, that even by manually excluding the padding, the highlight when pressing the button would be cut off, because it is a bit bigger than the button itself. But this is not the case and it works perfectly. The button now gets displayed in its whole glory. ;)

You can overload your onMeasure method to always return a square.

Create a class that extends to Button and include this

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  int size = Math.min(getMeasuredWidth(), getMeasuredHeight());
  setMeasuredDimension(size, size);
}

Not sure how this will work if you give exact dimensions but it should work if you set width, height to match parent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!