0

我想为具有 2 种状态的路线规划目的制作一个接口 1 - 输入起始地址和目标地址 2 - 通过 GPS 定位服务做同样的事情

我已经通过 XML 文件进行了内容布局,但由于我需要在它们之间动态切换,我尝试通过 Java 按钮事件设置相同的东西。但是,每当我尝试通过 Java 在内部设置带有 EditText 的 TextInputLayout 并尝试在我的 Android 模拟器上编译和启动它时,它是一个 Pixel 2 API lvl 28,它给了我一个 java.lang.IllegalArgumentError 消息“样式开启”此组件要求您的应用主题为 Theme.AppCompat(或后代)'。

    inputTop = new android.support.design.widget.TextInputLayout(getApplicationContext());
    inputTop.setLayoutParams(new ConstraintLayout.LayoutParams(Constraints.LayoutParams.FILL_PARENT - 120, (int) convertDpToPx(getApplicationContext(), 45)));
    inputTop.setX(convertDpToPx(getApplicationContext(), 174));
    inputTop.setY(convertDpToPx(getApplicationContext(), 60));

    inputTopInner = new EditText(getApplicationContext());
    inputTopInner.setLayoutParams(new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    inputTopInner.setGravity(Gravity.CENTER);
    inputTopInner.setInputType(InputType.TYPE_CLASS_TEXT);
    inputTopInner.setTextColor(Color.rgb(151,151,151));
    inputTopInner.setTextSize(12);
    inputTopInner.setHint("Standort");
    inputTopInner.setEms(10);
    inputTop.addView(inputTopInner);

    layout.addView(viewTop);
    layout.addView(inputTop);

有人知道如何使它像输入 XML 文件时那样运行吗?

4

1 回答 1

1

这就是我们为我们的项目所做的:

我们将 Parent 保留为 LinearLayout 并在该布局中添加视图

 LiearLayout parentView = findViewById(R.id.parentView);

            TextInputLayout emailTextInputLayout = new TextInputLayout(this, null, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox);

            emailTextInputLayout.setHint("Please Enter Email Address");
            emailTextInputLayout.setBoxBackgroundMode(TextInputLayout.BOX_BACKGROUND_OUTLINE);
            emailTextInputLayout.setBoxCornerRadii(5, 5, 5, 5);
            TextInputEditText edtEmail = new TextInputEditText(emailTextInputLayout.getContext());
            emailTextInputLayout.addView(edtEmail);

            parentView.addView(emailTextInputLayout);


            TextInputLayout passTextInputLayout = new TextInputLayout(this, null, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox);

            passTextInputLayout.setHint("Please Enter Password");
            passTextInputLayout.setBoxBackgroundMode(TextInputLayout.BOX_BACKGROUND_OUTLINE);
            passTextInputLayout.setBoxCornerRadii(5, 5, 5, 5);
            TextInputEditText edtPass = new TextInputEditText(passTextInputLayout.getContext());
            passTextInputLayout.addView(edtPass);

            parentView.addView(passTextInputLayout);
于 2019-06-03T09:54:52.570 回答