【问题标题】:How to align vertical textview and editText in Linearlayout in android如何在android的Linearlayout中对齐垂直textview和editText
【发布时间】:2017-07-08 05:30:50
【问题描述】:

目前,我正在尝试使用 android studio 中的 Linearlayout 编写 textview 和 edittext 以垂直对齐。我尝试设置布局重力和其他元素,但目前无法实现我想要的,这是我的代码。

LinearLayout params3 = new LinearLayout(this);
        params3.setOrientation(LinearLayout.VERTICAL);
        params3.setGravity(Gravity.CENTER_VERTICAL);

        LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        params2.weight = 35;
        params3.setLayoutParams(params2);
        params2.height = convertDiptoPix(80,context);
        //params2.leftMargin = convertDiptoPix(5,context);


        TextView textView1 = new TextView(this);
        textView1.setId(View.generateViewId());
        textView1.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
        textView1.setPadding(0,convertDiptoPix(1,context),0,convertDiptoPix(3,context));
        textView1.setText("Receipt");
        textView1.setTextColor(Color.parseColor("#000000"));
        //textView1.setLayoutParams(params2);
        textView1.setTextSize(convertDiptoPix(13,context));

        EditTextIME editTextIME = new EditTextIME(this);
        editTextIME.setId(View.generateViewId());
        //editTextIME.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        //editTextIME.setMinimumHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        editTextIME.setGravity(Gravity.BOTTOM | Gravity.CENTER_VERTICAL);
        //editTextIME.setLayoutParams(params2);
        editTextIME.setFocusableInTouchMode(true);
        editTextIME.setKeyListener(DigitsKeyListener.getInstance("abcdefghijklmnopqrstuvwxyzöäüßñéúíóáèùòìàâãåæçëêìíîïôõûýABCDEFGHIJKLMNOPQRSTUVWXYZÖÄÜÕÔÒÔÓÙÚÛÝÑÇÈÉÊËÌÍÎÏÆÅÄÃÂÁÀØ01234567890()&*$%#!+-:;,._?¿/><[]{}|\\^ "));
        editTextIME.setHint("Notes");
        editTextIME.setInputType(InputType.TYPE_CLASS_TEXT);
        editTextIME.setHorizontallyScrolling(true);
        editTextIME.setTextSize(convertDiptoPix(13,context));

有没有人尝试在 android studio 中编写类似垂直对齐的代码?你能分享你的想法吗?

【问题讨论】:

  • 请问params2.weight = 35;..的目的是什么?用xml写有什么问题吗?
  • 它们目前是如何对齐的?我没有看到您将文本视图和编辑文本视图添加到线性布局。

标签: java android android-layout runtime


【解决方案1】:

你可以试试这个,

        LinearLayout parent = new LinearLayout(context);

        parent.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        parent.setOrientation(LinearLayout.VERTICAL);

        LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        TextView textView1 = new TextView(this);
        textView1.setLayoutParams(params2);
        textView1.setId(View.generateViewId());
        textView1.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
        textView1.setText("Receipt");
        textView1.setTextColor(Color.parseColor("#000000"));

        EditText editTextIME = new EditText(this);
        editTextIME.setId(View.generateViewId());
        editTextIME.setGravity(Gravity.BOTTOM | Gravity.CENTER_VERTICAL);
        editTextIME.setLayoutParams(params2);
        editTextIME.setFocusableInTouchMode(true);
        editTextIME.setKeyListener(DigitsKeyListener.getInstance("abcdefghijklmnopqrstuvwxyzöäüßñéúíóáèùòìàâãåæçëêìíîïôõûýABCDEFGHIJKLMNOPQRSTUVWXYZÖÄÜÕÔÒÔÓÙÚÛÝÑÇÈÉÊËÌÍÎÏÆÅÄÃÂÁÀØ01234567890()&*$%#!+-:;,._?¿/><[]{}|\\^ "));
        editTextIME.setHint("Notes");
        editTextIME.setInputType(InputType.TYPE_CLASS_TEXT);
        editTextIME.setHorizontallyScrolling(true);
//        editTextIME.setTextSize(convertDiptoPix(13,context));

        parent.addView(textView1);
        parent.addView(editTextIME);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-05
    • 1970-01-01
    • 2011-06-13
    相关资源
    最近更新 更多