android自定义控件,我想做一个自定义相对布局控件中,包含两个子TextView
android自定义控件,我想做一个自定义相对布局控件中,包含两个子TextView,这个两个TextView要在代码中生成,这两个TextView高度充满父控件并等分;...
android自定义控件,我想做一个自定义相对布局控件中,包含两个子TextView,这个两个TextView要在代码中生成,这两个TextView高度充满父控件并等分;问:怎么能够通过设置父控件的高度来控制TextView的高度2
展开
2个回答
展开全部
你可以get父控件拆做的layoutparams,然后取到里面的高,然后通过这个高,来定义一个layoutparams, set给你的textView便得了,我写的方法给你参考下:
/**
* 将传进来view的布局参数按照比例缩放,以适应旅闹衡不同的屏幕大小,这里处理了RelativeLayout、FrameLayout、
* LinearLayout三种容器里View的布局参数,如有需要可自行增减弯裤,其中的scaleWidth=现设备宽/原设备宽
* scaleHeight = 现设备高/原设备高,这个需要用displaymetrics类来取
* @param view
* @param scaleWidth
* @param scaleHeight
*/
public static void setParams(View view, float scaleWidth, float scaleHeight) {
RelativeLayout.LayoutParams rlParams = null;
FrameLayout.LayoutParams flParams = null;
LinearLayout.LayoutParams llParams = null;
if (view.getParent() instanceof RelativeLayout) {
rlParams = (RelativeLayout.LayoutParams) (view.getLayoutParams());
rlParams.width = (int) (rlParams.width * scaleWidth);
rlParams.height = (int) (rlParams.height * scaleHeight);
rlParams.leftMargin = (int) (rlParams.leftMargin * scaleWidth);
rlParams.rightMargin = (int) (rlParams.rightMargin * scaleWidth);
rlParams.topMargin = (int) (rlParams.topMargin * scaleHeight);
rlParams.bottomMargin = (int) (rlParams.bottomMargin * scaleHeight);
view.setLayoutParams(rlParams);
} else if (view.getParent() instanceof FrameLayout) {
flParams = (FrameLayout.LayoutParams) (view.getLayoutParams());
flParams.width = (int) (flParams.width * scaleWidth);
flParams.height = (int) (flParams.height * scaleHeight);
flParams.leftMargin = (int) (flParams.leftMargin * scaleWidth);
flParams.rightMargin = (int) (flParams.rightMargin * scaleWidth);
flParams.topMargin = (int) (flParams.topMargin * scaleHeight);
flParams.bottomMargin = (int) (flParams.bottomMargin * scaleHeight);
view.setLayoutParams(flParams);
} else if (view.getParent() instanceof LinearLayout) {
llParams = (LinearLayout.LayoutParams) (view.getLayoutParams());
llParams.width = (int) (llParams.width * scaleWidth);
llParams.height = (int) (llParams.height * scaleHeight);
llParams.leftMargin = (int) (llParams.leftMargin * scaleWidth);
llParams.rightMargin = (int) (llParams.rightMargin * scaleWidth);
llParams.topMargin = (int) (llParams.topMargin * scaleHeight);
llParams.bottomMargin = (int) (llParams.bottomMargin * scaleHeight);
view.setLayoutParams(llParams);
}
}
/**
* 将传进来view的布局参数按照比例缩放,以适应旅闹衡不同的屏幕大小,这里处理了RelativeLayout、FrameLayout、
* LinearLayout三种容器里View的布局参数,如有需要可自行增减弯裤,其中的scaleWidth=现设备宽/原设备宽
* scaleHeight = 现设备高/原设备高,这个需要用displaymetrics类来取
* @param view
* @param scaleWidth
* @param scaleHeight
*/
public static void setParams(View view, float scaleWidth, float scaleHeight) {
RelativeLayout.LayoutParams rlParams = null;
FrameLayout.LayoutParams flParams = null;
LinearLayout.LayoutParams llParams = null;
if (view.getParent() instanceof RelativeLayout) {
rlParams = (RelativeLayout.LayoutParams) (view.getLayoutParams());
rlParams.width = (int) (rlParams.width * scaleWidth);
rlParams.height = (int) (rlParams.height * scaleHeight);
rlParams.leftMargin = (int) (rlParams.leftMargin * scaleWidth);
rlParams.rightMargin = (int) (rlParams.rightMargin * scaleWidth);
rlParams.topMargin = (int) (rlParams.topMargin * scaleHeight);
rlParams.bottomMargin = (int) (rlParams.bottomMargin * scaleHeight);
view.setLayoutParams(rlParams);
} else if (view.getParent() instanceof FrameLayout) {
flParams = (FrameLayout.LayoutParams) (view.getLayoutParams());
flParams.width = (int) (flParams.width * scaleWidth);
flParams.height = (int) (flParams.height * scaleHeight);
flParams.leftMargin = (int) (flParams.leftMargin * scaleWidth);
flParams.rightMargin = (int) (flParams.rightMargin * scaleWidth);
flParams.topMargin = (int) (flParams.topMargin * scaleHeight);
flParams.bottomMargin = (int) (flParams.bottomMargin * scaleHeight);
view.setLayoutParams(flParams);
} else if (view.getParent() instanceof LinearLayout) {
llParams = (LinearLayout.LayoutParams) (view.getLayoutParams());
llParams.width = (int) (llParams.width * scaleWidth);
llParams.height = (int) (llParams.height * scaleHeight);
llParams.leftMargin = (int) (llParams.leftMargin * scaleWidth);
llParams.rightMargin = (int) (llParams.rightMargin * scaleWidth);
llParams.topMargin = (int) (llParams.topMargin * scaleHeight);
llParams.bottomMargin = (int) (llParams.bottomMargin * scaleHeight);
view.setLayoutParams(llParams);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |