如何以编程方式将文本设置为编辑文本?

时间:2015-04-03 11:28:45

标签: android

我只想将编辑文本的TextView设置为编辑文本的左侧。我搜索了很多,我还没有找到解决方案,我不知道我的错误。

这是我的代码:

        LinearLayout ll_text = new LinearLayout(this);
            LayoutParams ll_text_params = new LayoutParams(width,
                    height);
            ll_text.setGravity(Gravity.CENTER);
            ll_text.setOrientation(LinearLayout.VERTICAL);
            ll_text.setId(Integer.parseInt(question_id));
            //idOfRel++;

            TextView tv_question_text = new TextView(this);
            LayoutParams text_question_params = new LayoutParams(
                    android.view.ViewGroup.LayoutParams.FILL_PARENT,
                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
            text_question_params.setMargins(Math.round(82 * multiplier), 0,
                    Math.round(82 * multiplier), 0);
            tv_question_text.setGravity(Gravity.CENTER);

            tv_question_text.setId(Integer.parseInt(question_id));
            tv_question_text.setTextSize(30.0f);
            tv_question_text.setText(question_no+"."+getTitleString(question_id, tv_question_text));
            tv_question_text.setTextColor(Color.WHITE);
            tv_question_text.setTypeface(nexa_bold);

            TextView tv_at=new TextView(this);
            LayoutParams tv_at_params=new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
            tv_at_params.setMargins(Math.round(20*multiplier),0, Math.round(290*multiplier),0);
            tv_at_params.topMargin = Math.round(30 * multiplier);
            tv_at.setId(6);
            tv_at.setText("@");
            tv_at.setTextSize(40.0f);



            ActionEditText et_text = new ActionEditText(this);
            RelativeLayout.LayoutParams et_text_params = new RelativeLayout.LayoutParams(
                    Math.round(472 * multiplier), Math.round(63 * multiplier));
    //      et_text_params.setMargins(Math.round(30*multiplier),0, Math.round(270*multiplier), 0);
            et_text_params.addRule(RelativeLayout.RIGHT_OF, 6);
            et_text.setId(1);
            et_text.setPadding(5, 0, 5, 0);
            et_text.setFilters(new InputFilter[] { new InputFilter.LengthFilter(40) });
            et_text.setHint("Twitter Name");
            et_text.setSingleLine(true);
            et_text.setPaddingRelative(15, 0, 15, 0);
            et_text.setTextSize(20.0f);
            et_text.setImeOptions(EditorInfo.IME_ACTION_DONE);

需要提前帮助...

enter image description here

像这样......

2 个答案:

答案 0 :(得分:1)

将此行添加到您的editText:

editText.setGravity(Gravity.LEFT);

答案 1 :(得分:0)

在第一个TextView下面再使用一个LinearLayout,并将第二个TextView和EditText添加到此LinearLayout。像这样:

LinearLayout ll_text = new LinearLayout(this);
    LayoutParams ll_text_params = new LayoutParams(width,
            height);
    ll_text.setGravity(Gravity.CENTER);
    ll_text.setOrientation(LinearLayout.VERTICAL);
    ll_text.setId(Integer.parseInt(question_id));
    //idOfRel++;

    TextView tv_question_text = new TextView(this);
    LayoutParams text_question_params = new LayoutParams(
            android.view.ViewGroup.LayoutParams.FILL_PARENT,
            android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
    text_question_params.setMargins(Math.round(82 * multiplier), 0,
            Math.round(82 * multiplier), 0);
    tv_question_text.setGravity(Gravity.CENTER);

    tv_question_text.setId(Integer.parseInt(question_id));
    tv_question_text.setTextSize(30.0f);
    tv_question_text.setText(question_no+"."+getTitleString(question_id, tv_question_text));
    tv_question_text.setTextColor(Color.WHITE);
    tv_question_text.setTypeface(nexa_bold);

    TextView tv_at=new TextView(this);
    LayoutParams tv_at_params=new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
            android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
    tv_at_params.setMargins(Math.round(20*multiplier),0, Math.round(290*multiplier),0);
    tv_at_params.topMargin = Math.round(30 * multiplier);
    tv_at.setId(6);
    tv_at.setText("@");
    tv_at.setTextSize(40.0f);



    ActionEditText et_text = new ActionEditText(this);
    RelativeLayout.LayoutParams et_text_params = new RelativeLayout.LayoutParams(
            Math.round(472 * multiplier), Math.round(63 * multiplier));
//      et_text_params.setMargins(Math.round(30*multiplier),0, Math.round(270*multiplier), 0);
        et_text_params.addRule(RelativeLayout.RIGHT_OF, 6);
        et_text.setId(1);
        et_text.setPadding(5, 0, 5, 0);
        et_text.setFilters(new InputFilter[] { new InputFilter.LengthFilter(40) });
        et_text.setHint("Twitter Name");
        et_text.setSingleLine(true);
        et_text.setPaddingRelative(15, 0, 15, 0);
        et_text.setTextSize(20.0f);
        et_text.setImeOptions(EditorInfo.IME_ACTION_DONE);

    //Additional Code to your code

    LinearLayout innerContainer = new LinearLayout(this);
    LayoutParams innerContainerParams = new LayoutParams(match_parent, wrap_content);
    innerContainerParams.setOrientation(LinearLayout.HORIZONTAL);
    innerContainer.addView(tv_at);
    innerContainer.addView(et_text);

    ll_text.addView(tv_question_text);
    ll_text.addView(innerContainer);

我猜你在找这样的东西。

et_text.setCompoundDrawablesWithIntrinsicBounds(R.drawable.myDrawable, 0, 0, 0);