gpt4 book ai didi

Android addView 将 TextView textColor 设置为白色?

转载 作者:行者123 更新时间:2023-11-30 00:54:01 29 4
gpt4 key购买 nike

我有一个按钮调用此函数以将 View 添加到我的主布局:

private void createWrkHist() {
View addBtn = findViewById(R.id.addWrkHist);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) addBtn.getLayoutParams();
int aboveID = params.getRule(BELOW);

LayoutInflater inflator = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.work_history_table, null);
ViewGroup mainView = (ViewGroup) findViewById(R.id.mainForm);
RelativeLayout.LayoutParams insertParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
insertParams.addRule(BELOW, aboveID);
v.setLayoutParams(insertParams);
mainView.addView(v);
params.addRule(BELOW, R.id.wrkHist);
addBtn.setLayoutParams(params);
}

正在添加布局,因为我使用了一些代码来尝试关注添加的 View 上的 EditText 并且它起作用了。但是,添加的 View 中的所有 TextViews 都是白色的。如何将要添加的 View 的样式/主题设置为默认值?这是我要添加的布局的 XML:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/wrkHist"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TableRow>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="Position:"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small" />

<EditText
android:id="@+id/wrkPosition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_weight="15" />
</TableRow>

<TableRow>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="Company:"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small" />

<EditText
android:id="@+id/wrkCompany"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_weight="15" />
</TableRow>

<TableRow>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight=".5"
android:text="Start Date:"
android:textAlignment="textEnd"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small" />

<LinearLayout android:layout_weight="15">

<EditText
android:id="@+id/wrkStartDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_weight="7"
android:inputType="date" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="End Date:"
android:textAlignment="textEnd"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small" />

<EditText
android:id="@+id/wrkEndDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_weight="7"
android:inputType="date" />
</LinearLayout>
</TableRow>

<TableRow>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="Duties:"
android:textAlignment="textEnd"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small" />

<EditText
android:id="@+id/wrkDuties"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_weight="15"
android:lines="5" />
</TableRow>
</TableLayout>

最佳答案

不要使用应用程序的上下文来创建 inflater,因为它会尝试使用应用程序的主题。

改为使用 Activity 的上下文,或者使用容器的上下文,它也会为您提供 Activity 的上下文。

private void createWrkHist() {
View addBtn = findViewById(R.id.addWrkHist);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) addBtn.getLayoutParams();
int aboveID = params.getRule(BELOW);

ViewGroup mainView = (ViewGroup) findViewById(R.id.mainForm);

LayoutInflater inflator = LayoutInflater.from(mainView.getContext());
View v = inflator.inflate(R.layout.work_history_table, mainView, true);

RelativeLayout.LayoutParams insertParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
insertParams.addRule(BELOW, aboveID);
v.setLayoutParams(insertParams);
mainView.addView(v);
params.addRule(BELOW, R.id.wrkHist);
addBtn.setLayoutParams(params);
}

关于Android addView 将 TextView textColor 设置为白色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40476562/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com