suwu150
suwu150
2016-04-06 14:57
采纳率: 44.3%
浏览 2.0k

Android中如何在maiactivity.java中往布局中添加一个文本编辑框

怎么从maiactivity.java中往布局中添加一个文本编辑框

EditText et_miyue=new EditText(null);
        et_miyue.setHint("请输入密钥...");
        et_miyue.setSingleLine(true);
        et_miyue.setAlpha(getWallpaperDesiredMinimumWidth());
        et_miyue.setX(BIND_ABOVE_CLIENT);
        addContentView(et_miyue,null);
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • lxk_1993
    103style 2016-04-08 02:39
    已采纳

    EditText et_miyue=new EditText(null);
    et_miyue.setHint("请输入密钥...");
    et_miyue.setSingleLine(true);
    et_miyue.setAlpha(getWallpaperDesiredMinimumWidth());
    et_miyue.setX(BIND_ABOVE_CLIENT);
    LinearLayout linear = new LinearLayout(this);

    //注意,对于LinearLayout布局来说,设置横向还是纵向是必须的!否则就看不到效果了。

    linear.setOrientation(LinearLayout.VERTICAL);

    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

    linear.addView(et_miyue, lp);

    setContentView(linear);

    点赞 评论
  • Wei_Smile
    李牧羊 2016-04-06 15:20

    使用LayoutParams

    点赞 评论
  • bruse_android
    bruse_android 2016-04-06 15:45

    EditText et=new EditText(this);
    通过LayoutParams设置属性然后进行addView

    点赞 评论
  • suwu150
    suwu150 2016-04-06 23:53

    [java] view plain copy print?
    Button button = new Button(this);

    button.setText("One");

    //此处相当于布局文件中的Android:gravity属性

    button.setGravity(Gravity.CENTER);

    LinearLayout linear = new LinearLayout(this);

    //注意,对于LinearLayout布局来说,设置横向还是纵向是必须的!否则就看不到效果了。

    linear.setOrientation(LinearLayout.VERTICAL);

    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

    //此处相当于布局文件中的Android:layout_gravity属性

    lp.gravity = Gravity.RIGHT;

    linear.addView(button, lp);

    setContentView(linear);

    点赞 评论

相关推荐