myc_100 2013-03-25 09:24 采纳率: 10%
浏览 5176
已采纳

在 Linearlayout 中如何在图像布局的右边,动态的把文本布局对齐?

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 的左边,但是如何以代码的形式实现呢?

  • 写回答

1条回答 默认 最新

  • hxn_217 2013-03-26 01:47
    关注

    解决你的问题有两种方法。如果你想在 textview 的右边显示图像,使用下面的代码:

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

    或者,把 linearlayout设置成横向的,首先添加 imageview,然后再添加 textview。就用按照你需要的对齐:

     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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题