[英]Dynamically added EditText ignores theme
I have one EditText defined in xml and I want to add another one dynamically but the dynamically added EditText doesn't use the theme as EditText added using xml. 我在xml中定义了一个EditText,我想动态添加另一个,但是动态添加的EditText不会使用主题,因为使用xml添加了EditText。 The 2 EditTexts look differently, which isn't desired.
2个EditText外观不同,这是不希望的。 See the image.
看图像。
I tried it on API 28 and also on physical device Samsung S8+ API 26. 我在API 28和物理设备Samsung S8 + API 26上进行了尝试。
Any idea how to fix this? 知道如何解决这个问题吗? I don't see a reason why theme is ignored and as long as I know I can't apply theme to EditText?
我看不出忽略主题的原因,只要我知道我不能将主题应用于EditText?
Code: 码:
public class TActivity extends AppCompatActivity {
private LinearLayout Layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.t);
Layout = (LinearLayout) findViewById(R.id.Layout);
EditText editText = new EditText(this);
editText.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
editText.setInputType(InputType.TYPE_CLASS_TEXT);
editText.setEnabled(true);
editText.setTextIsSelectable(true);
Layout.addView(editText);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/ThisWorks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"/>
</LinearLayout>
<activity android:name=".UI.TActivity" android:windowSoftInputMode="adjustResize" android:theme="Theme.AppCompat.Light.NoActionBar" />
In my app, I'm adding views dynamically by inflating an xml layout file, and there are no issues with themes. 在我的应用程序中,我通过扩展xml布局文件来动态添加视图,并且主题没有问题。 This is a piece of code that I use:
这是我使用的一段代码:
View newView = LayoutInflater.from(context).inflate(R.layout.field, null);
newView.setId(ViewIdGenerator.generateViewId()); //This is a custom method
//that sets Id for a newly created view
//to find it later on for usage
ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.MATCH_PARENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT);
newView.setLayoutParams(params);
Because I create here a ConstraintLayout and you need an EditText, you should probably create a layout xml file with an EditText as its root, like the one below, and then inflate it. 因为我在这里创建了ConstraintLayout,并且需要一个EditText,所以您应该创建一个以EditText为根的版面xml文件,就像下面的一样,然后对其进行充气。
<?xml version="1.0" encoding="utf-8"?>
<EditText
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ThisWorks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine">
</EditText>
I'm not sure if it works for you, but you might want to give it a try. 我不确定它是否适合您,但您可能想尝试一下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.