国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

獲取控件大小和設置調整控件的位置XY示例

2020-04-11 12:07:42
字體:
來源:轉載
供稿:網友
網上很多人對設置控件的位置都使用view.setPadding(left, top, right, bottom) ,其實這玩意很差勁,它是設置自己本身位置的偏移,我們很少需要這種效果,我需要的設置控件相對屏幕左上角的X 、Y位置。眾里尋他千百度,驀然回首,那人卻在燈火闌珊處!
復制代碼 代碼如下:

import android.view.View;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.RelativeLayout;
/*
* 獲取、設置控件信息
*/
public class WidgetController {
/*
* 獲取控件寬
*/
public static int getWidth(View view)
{
int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
view.measure(w, h);
return (view.getMeasuredWidth());
}
/*
* 獲取控件高
*/
public static int getHeight(View view)
{
int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
view.measure(w, h);
return (view.getMeasuredHeight());
}

/*
* 設置控件所在的位置X,并且不改變寬高,
* X為絕對位置,此時Y可能歸0
*/
public static void setLayoutX(View view,int x)
{
MarginLayoutParams margin=new MarginLayoutParams(view.getLayoutParams());
margin.setMargins(x,margin.topMargin, x+margin.width, margin.bottomMargin);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(margin);
view.setLayoutParams(layoutParams);
}
/*
* 設置控件所在的位置Y,并且不改變寬高,
* Y為絕對位置,此時X可能歸0
*/
public static void setLayoutY(View view,int y)
{
MarginLayoutParams margin=new MarginLayoutParams(view.getLayoutParams());
margin.setMargins(margin.leftMargin,y, margin.rightMargin, y+margin.height);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(margin);
view.setLayoutParams(layoutParams);
}
/*
* 設置控件所在的位置YY,并且不改變寬高,
* XY為絕對位置
*/
public static void setLayout(View view,int x,int y)
{
MarginLayoutParams margin=new MarginLayoutParams(view.getLayoutParams());
margin.setMargins(x,y, x+margin.width, y+margin.height);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(margin);
view.setLayoutParams(layoutParams);
}
}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 云安县| 兴安盟| 娄烦县| 台北县| 新疆| 随州市| 北碚区| 华池县| 德保县| 义乌市| 涞源县| 夏邑县| 九台市| 汽车| 沧州市| 哈尔滨市| 六枝特区| 汾阳市| 左贡县| 朝阳区| 安溪县| 林甸县| 广德县| 日照市| 六安市| 兴宁市| 富锦市| 瑞金市| 高雄县| 通化市| 莱阳市| 秭归县| 大渡口区| 乌兰浩特市| 桐城市| 北票市| 望谟县| 钟山县| 盐亭县| 高台县| 武陟县|