0
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout3);
TextView tv = new TextView(this);
ImageButton imb=new ImageButton(this);

ll.addView(tv);
tv.setSingleLine();

tv.setEllipsize(TruncateAt.MARQUEE);
tv.setHorizontallyScrolling(true);
tv.setFocusableInTouchMode(true);

tv.setText(myString1);

这是我的代码,我现在可以显示数据了textView。但我想imageButton在左边放一个,textView这样就可以作为取消条。但我有一个问题。如何imageButton以编程方式设置文本视图的左侧?

4

1 回答 1

0

您的问题有两个解决方案。如果您只想在 textview 右侧显示图像,可以使用以下代码。

TextView textView = (TextView) findViewById(R.id.myTxtView);
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon,0, 0,0);

要不然

将您的线性布局的方向设置为水平并首先添加图像视图,然后添加文本视图。因此,它们将根据您的要求进行调整。

下面是示例。

   TextView tv = new TextView(this);
tv.setLayoutParams(new ViewGroup.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT));
   ImageButton imb=new ImageButton(this);
imb.setLayoutParams(new ViewGroup.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT));

   ll.addView(imb);
   ll.addView(tv);
于 2012-12-26T10:22:27.053 回答