android中怎么获取一个视屏在view中的有效高宽。

android中怎么获取一个视屏在view中的有效高宽。,第1张

1

WindowManager windowManager = getWindowManager();

Display display = windowManagergetDefaultDisplay();

screenWidth = displaygetWidth();

screenHeight = displaygetHeight();

2

DisplayMetrics dm = new DisplayMetrics();

thisgetWindowManager()getDefaultDisplay()getMetrics(dm);//this指当前activity

screenWidth =dmwidthPixels;

screenHeight =dmheightPixels;

以上两种方法在屏幕未显示的时候,还是处于0的状态,即要在setContentView调用之后才有效。

3

还可以在onDraw中由canvas来获得

screenWidth =canvasgetWidth();

screenHeight =canvasgetHeight();

parent是你要对齐的控件

比如显示在上面:

int offsetY = -parentViewgetHeight()-popWingetHeight();

popWinshowAsDropDown(parentView, 0, offsetY);

左面和右面差不多,计算宽度即可

popUpWindowsetBackgroundDrawable(getResources()getDrawable(Rdrawablepage2));

设置的是popupwindow(window容器)的背景。

popUpWindow

=

new

PopupWindow(show_popvieView,LayoutParamsWRAP_CONTENT,LayoutParamsWRAP_CONTENT);

//是将show_popvieView放入容器中,以自适应作为大小,且容器也采用自适应。

综上,如果你设置大小,导致show_popvieView沾满整个屏幕,那么window容器最为底层,设置的背景坑定是看不见的。

建议:背景设置采用设置show_popvieView的背景。如果有多层,可以在内容里面镶嵌,最好别直接设置外层popupwindow容器

这样看你listview显示item的布局,如果整个布局下只有一个Listview,TextView和EditText是一上一下的话,直接match_parent即可(最好截个图或把布局代码发出来)。获取listview的item的高度和宽度我提供一个代码:

public static void getListItemWH(ListView listView) {

    try {

        //获取ListView对应的Adapter

        ListAdapter listAdapter = listViewgetAdapter();

        if (listAdapter == null) {

            return;

        }

        int len = listAdaptergetCount();//item总数

        for (int i = 0; i < len; i++) {

            View listItem = listAdaptergetView(i, null, listView);

            if (null != listItem) {

                listItemmeasure(0, 0);//计算子项View的宽高

                int h = listItemgetMeasuredHeight();//每一个item的高度

                int w = listItemgetMeasuredHeight();//每一个item的宽度

                Logd("test", "h=" + h "; w=" + w);//打印看看

            }

        }

        listViewgetDividerHeight();//获取子项间分隔符占用的高度

    } catch (Exception e) {    

    }

}

//设置控件高度为宽度的一半

//screenWidth 为屏幕的宽度

//View为你要设置的控件

//LinearLayoutLayoutParams 为这个控件外部的布局

LinearLayoutLayoutParams layoutParams = (LinearLayoutLayoutParams) ViewgetLayoutParams();

layoutParamsheight = screenWidth / 2;

Android动态改变View控件大小的方法:

1、声明控件参数获取对象 LayoutParams lp;

2、获取控件参数: lp = 控件idgetLayoutParams();

3、设置控件参数:如高度。 lpheight -= 10;

4:、使设置生效:控件idsetLayoutParams(lp);

例如如要把Imageview下移200px: ImageViewsetPadding( ImageViewgetPaddingLeft(), ImageViewgetPaddingTop()+200, ImageViewgetPaddingRight(), ImageViewgetPaddingBottom());

以上就是关于android中怎么获取一个视屏在view中的有效高宽。全部的内容,包括:android中怎么获取一个视屏在view中的有效高宽。、android 怎样得到一个控件未画出来的大小、android的popupwindow控件的大小的问题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9796509.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-02
下一篇2023-05-02

发表评论

登录后才能评论

评论列表(0条)

    保存