我有一个调用此函数的按钮,可以将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);
}
正在添加布局,因为我使用了一些代码来尝试并专注于添加的视图上的EditText
并且它有效。但是,添加的视图中的所有TextViews
都是完全白色的。如何设置我添加到默认视图的样式/主题?这是我添加的布局的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>
答案 0 :(得分:1)
不要使用Application的上下文来创建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);
}