关于LayoutParams:
LayoutParams相当于一个Layout的信息报,它保存了一个View的布局参数,比如高、宽、边距等信息。
使用getLayoutParams()方法可以获得控件的LayoutParams信息,例如:
(1)获得ImageView控件的LayoutParams信息:
ImageView imageView = new ImageView(getContext());
//ImageView 以 LinearLayout为父布局
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)
imageView.getLayoutParams();
(2)获得布局的LayoutParams信息:
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) getLayoutParams();
使用setLayoutParams()方法则可把动态设置的布局参数加入到目标布局中。
使用LayoutParams:
如上提到,使用时首先要注意父布局,即父布局
(1)如果是LinearLayout,使用方式为:
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) getLayoutParams();
(2) 如果是RelativeLayout,使用方式为:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) getLayoutParams();
(3) 如果是ConstraintLayout,使用方式为:
ConstraintLayout.LayoutParams params = (LayoutParams) getLayoutParams();
代码实例:
一、动态添加单个控件
如果不依赖其他布局参数,想把某一新控件放到一个现有布局中时,需要先自行创建该控件和一个新布局参数集LayoutParams,然后再把新控件通过addView()方法添加到当前布局上。接下来通过代码实例进行具体说明:
对应的效果图如下:
二、动态添加两个控件
这个实例我通过当前做的一个小Demo来展示吧,我的目的是想让一个ImageView和一个TextView跟随ScrollView视图内文字高度动态加载。ImageView的背景放置一个百度的logo,TextView内显示文字为:数据来源。先看下效果后面上代码
//基类为Fragment,主布局类型为ConstraintLayout
public class GraphicBaikeView extends BaseFragment {
private ScrollView mScrollView;
private TextView mTitleText,mContentText,mLogoText;
private ImageView mSourceImage,mLogoImage;
private int mHeight = 0;
private ConstraintLayout mConstraintLayout;
//创建当前类实例,KEY_DATA为父类BaseFragment属性,通过bundle传递data数据后在父类onCreate时通过getArguments()获取
public static BaseFragment newInstance(String data) {
BaseFragment fragment = new GraphicBaikeView();
Bundle bundle = new Bundle();
bundle.putString(KEY_DATA,data);
fragment.setArguments(bundle);
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//加载ConstraintLayout 布局文件view_baike_graphic.xml
View view = inflater.inflate(R.layout.view_baike_graphic,container,false);
initView(view);
return view;
}
//初始化布局内的空间
private void initView(View view) {
mConstraintLayout = view.findViewById(R.id.root_baike_graphic);
mScrollView = view.findViewById(R.id.baike_scrollview);
mContentText = view.findViewById(R.id.baike_content);
mTitleText = view.findViewById(R.id.baike_title);
mSourceImage = view.findViewById(R.id.baike_image_source);
mLogoImage = view.findViewById(R.id.baike_image_logo);
mLogoText = view.findViewById(R.id.baike_text_logo);
parseData();
}
private void parseData() {
//mData数据是一个Json串,此处利用Gson方式对Json数据进行解析
BaikeBean baikeBean = BaikeBean.readValue(mData,BaikeBean.class);
// 获取文本最大高度
mScrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// 增加焦点获取, 防止无法响应按键
mScrollView.requestFocus();
//获得高度值
mHeight = mScrollView.getChildAt(0).getMeasuredHeight();
Log.d(TAG, "mHeight: " + mHeight);
mScrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
setSourceImage();
}
});
//利用Glide下载url图片,并对图片进行处理加载;若图片加载失败则加载默认图片
GlideLoader.getInstance().getManager().load(baikeBean.getPicUrl()).asBitmap().listener(new RequestListener<String, Bitmap>() {
@Override
public boolean onException(Exception e, String s, Target<Bitmap> target, boolean b) {
mSourceImage.setBackgroundResource(R.drawable.pic_default);
return false;
}
@Override
public boolean onResourceReady(final Bitmap bitmap, String s, Target<Bitmap> target, boolean b, boolean b1) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
setDownload(bitmap);
}
});
return false;
}
}).into(mSourceImage);
mTitleText.setText(baikeBean.getTitle());
mContentText.setText(baikeBean.getContent());
}
private void setSourceImage() {
mLogoText.setVisibility(View.GONE);
mLogoImage.setVisibility(View.GONE);
//当文本高度大于583时默认显示在view_baike_graphic.xml已定义好的空间位置(注意,在view_baike_graphic.xml中是隐藏状态,即android:visibility="gone")
if (mHeight >= 583) {
mLogoText.setVisibility(View.VISIBLE);
mLogoImage.setVisibility(View.VISIBLE);
} else {
//当文本高度小于583时开始动态加载百度logo和“数据来源”这两个控件
//具体方式同上一步中设置单个控件,如果想添加两个我是通过两个LayoutParams 实现
//第一个控件,宽度为80,高度为29
ConstraintLayout.LayoutParams imageLayoutParams = new
ConstraintLayout.LayoutParams(80,29);
ImageView logoImage = new ImageView(getContext());
imageLayoutParams.topToTop = Constraints.LayoutParams.PARENT_ID;
imageLayoutParams.rightToRight = Constraints.LayoutParams.PARENT_ID;
imageLayoutParams.width = 80;
imageLayoutParams.height = 29;
imageLayoutParams.topMargin = 102 + mHeight + 14;
imageLayoutParams.rightMargin = 46;
logoImage.setBackgroundResource(R.drawable.logo_baidu);
logoImage.setLayoutParams(imageLayoutParams);
mConstraintLayout.addView(logoImage);
//第二个控件,同样是宽度为80,高度为29
ConstraintLayout.LayoutParams textLayoutParams = new
ConstraintLayout.LayoutParams(80,29);
TextView logoText = new TextView(getContext());
textLayoutParams.topToTop = Constraints.LayoutParams.PARENT_ID;
textLayoutParams.rightToRight = Constraints.LayoutParams.PARENT_ID;
textLayoutParams.width = 80;
textLayoutParams.height = 29;
textLayoutParams.topMargin = 102 + mHeight + 14;
textLayoutParams.rightMargin = 129;
//设置颜色信息,也可以用另一种写法,logoText.setTextColor(0xFFFFFF),0x是颜色整数的标记,后面紧跟颜色值就行
logoText.setTextColor(this.getResources().getColor(R.color.baike_source_text));
logoText.setTextSize(20);
//设置透明度
logoText.setAlpha((float) 0.6);
logoText.setText("数据来自");
logoText.setLayoutParams(textLayoutParams);
mConstraintLayout.addView(logoText);
}
}
}
//处理图片
private void setDownload(Bitmap bitmap) {
Log.d(TAG, "setDownload");
if (bitmap != null && mSourceImage != null) {
android.util.Log.d(TAG, "bitmap Height = " + bitmap.getHeight());
android.util.Log.d(TAG, "bitmap Width = " + bitmap.getWidth());
int newWidth = 0;
int newHight = 0;
float n = (bitmap.getWidth()) / 280;
if ((bitmap.getHeight() / n) < 420) {
newWidth = 280;
newHight = (int) ((bitmap.getHeight()) / n);
} else if ((bitmap.getHeight() / n) == 420) {
newWidth = 280;
newHight = 420;
} else if ((bitmap.getHeight() / n) > 420) {
float m = (bitmap.getHeight() / 420);
newWidth = (int) ((bitmap.getWidth()) / m);
newHight = 420;
}
android.util.Log.d(TAG, "w--" + newWidth + "--h--" + newHight);
Bitmap bmp = zoomImgpic(bitmap, newWidth, newHight);
android.util.Log.d(TAG, "n w--" + bmp.getWidth() + "--h--" + bmp.getHeight());
mSourceImage.setImageBitmap(bmp);
mSourceImage.setVisibility(View.VISIBLE);
}
}
public Bitmap zoomImgpic(Bitmap bm, int newWidth, int newHeight) {
// 获得图片的宽高
int width = bm.getWidth();
int height = bm.getHeight();
// 计算缩放比例
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
if (scaleWidth >= scaleHeight) {
matrix.postScale(scaleHeight, scaleHeight);
} else if (scaleWidth < scaleHeight) {
matrix.postScale(scaleWidth, scaleWidth);
}
// 得到新的图片
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
true);
return newbm;
}
本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。