我必须以编程方式在textview的右上角设置图像.但它设置在textview的左侧.任何人都能告诉我如何处理这个问题吗?
我的代码如下:
FrameLayout frameLayout = KaHOUtility.generateFrameLayout(mContext);
frameLayout.setPadding(5,5,5);
LinearLayout linearLayout = KaHOUtility.generateLinearLayout(mContext);
KaHOTextView textView = KaHOUtility.generatePanelHeadingTextViews(mContext);
textView.setText(name);
linearLayout.addView(textView);
frameLayout.addView(linearLayout);
ImageView imageView = KaHOUtility.generateImageView(mContext,15,R.drawable.cancel_mark);
LinearLayout.LayoutParams rPrams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
rPrams.gravity = Gravity.RIGHT|Gravity.TOP ;
imageView.setLayoutParams(rPrams);
frameLayout.addView(imageView);
最佳答案
您正在从LinearLayout.LayoutParams类创建布局参数.但是,imageview正被添加到FrameLayout中.这是不正确的,因为您只将直接父级的布局参数应用于视图.所以,在你的情况下,它应该是:
原文链接:https://www.f2er.com/android/430034.htmlImageView imageView = KaHOUtility.generateImageView(mContext,R.drawable.cancel_mark);
FrameLayout.LayoutParams rPrams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT);
rPrams.gravity = Gravity.RIGHT | Gravity.TOP ;
imageView.setLayoutParams(rPrams);
frameLayout.addView(imageView);