以编程方式在textview的右上角设置imageview

时间:2016-08-11 07:44:42

标签: android android-layout android-fragments

我必须以编程方式在textview正确 顶部上设置图片。但它设置在textview的左侧。任何人都能告诉我如何处理这个问题吗? 我的代码如下:

  FrameLayout frameLayout = KaHOUtility.generateFrameLayout(mContext);
                frameLayout.setPadding(5, 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, 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);

1 个答案:

答案 0 :(得分:3)

您正在从LinearLayout.LayoutParams班级创建布局参数。但是,imageview正在添加到FrameLayout。这是不正确的,因为您只将直接父级的布局参数应用于视图。所以,在你的情况下,它应该是:

ImageView imageView = KaHOUtility.generateImageView(mContext, 15, 15, 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);