2016-11-07 85 views
1

我有一個按鈕調用該函數的視圖添加到我的主要佈局:Android的addView設置TextView textColor爲白色?

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> 

回答

1

不要使用應用程序上下文創建充氣,因爲它會嘗試使用應用程序的主題。

改爲使用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); 
} 
相關問題