2016-05-18 3 views
1

Ich befülle gerade ein TableLayout mit Daten aus externen Quellen. Nach dem programmgesteuerten Erstellen der TableRows und TextViews wird ihre Textfarbe auf Weiß festgelegt (scheint der Standardwert zu sein).Wie definiere ich die Textfarbe aller Kinder in einem Android Layout?

Ich weiß, dass ich die Farbe durch die Verwendung myTextView.setTextColor(Color.BLACK); einstellen kann, aber ich bin auf der Suche nach einer Möglichkeit, die Farbe in der XML-Layout-Datei zu definieren (Ich mag Visuals von der Logik trennen). Weiß jemand, wie man das macht?

Mein Layout (mit Beispiel Reihen gefüllt):

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:id="@+id/mytable" 
    android:stretchColumns="0,1,2" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    style="@style/AppTheme" > 

    <TableRow> 
    <TextView android:text="field1" /> 
    <TextView android:text="field2" /> 
    <TextView android:text="field3" /> 
    </TableRow> 
</TableLayout> 

Mein Java-Code:

public void updateTable() { 

    TableLayout table = (TableLayout) findViewById(R.id.mytable); 
    table.removeAllViewsInLayout(); 
    TableRow row = new TableRow(getApplicationContext()); 
    TextView text; 

    // Add Heading 
    text = new TextView(getApplicationContext()); 
    text.setText("Head1"); 
    text.setTextColor(this.textColor); 
    row.addView(text); 

    text = new TextView(getApplicationContext()); 
    text.setText("Head2"); 
    text.setTextColor(this.textColor); 
    row.addView(text); 

    text = new TextView(getApplicationContext()); 
    text.setText("Head3"); 
    text.setTextColor(this.textColor); 
    row.addView(text); 

    table.addView(row); 

    for(Data d : this.getData()) { 
    TableRow r = new TableRow(getApplicationContext()); 
    TextView t1 = new TextView(getApplicationContext()); 
    TextView t2 = new TextView(getApplicationContext()); 
    TextView t3 = new TextView(getApplicationContext()); 

    t1.setTextColor(this.textColor); 
    t2.setTextColor(this.textColor); 
    t3.setTextColor(this.textColor); 

    t1.setText(d.getData1()); 
    t2.setText(d.getData2()); 
    t3.setText(d.getData3()); 

    r.addView(t1); 
    r.addView(t2); 
    r.addView(t3); 
    table.addView(r); 
    } 
} 
+0

gerade eingestellten Textfarbe in Textview LeoMobDev

+0

Sie können ein Layout, das nur TextView enthält, mit der gewünschten Textfarbe auffüllen, anstatt programmgesteuert ein TextView zu erstellen. –

Antwort

1

1.Erstellen eine table_item.xml Datei von Textview

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/text1" 
    android:text="@string/hello_world" 
    android:textStyle="bold" 
    android:textColor="#ff00ff"/> 

2.Inflate table_item.xml

LayoutInflater inflater = LayoutInflater.from(getContext()); 
TextView text = (TextView)inflater.inflate(R.layout.table_item, this); 

3.Add in Tablelayout

r.addView(text); 
table.addView(r); 
+0

Das Aufblasen scheint das zu sein, wonach ich gesucht habe, danke! – Leifb

0

eine styles.xml erstellen:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<style name="MyTheme" parent="android:Theme"> 
    <item name="android:textViewStyle">@style/MyTextViewStyle</item> 
</style> 

<style name="MyTextViewStyle" parent="android:Widget.TextView"> 
    <item name="android:textColor">#F00</item> 
    <item name="android:textStyle">bold</item> 
</style> 
</resources> 

Dann bewerben Sie gerade das Thema Ihre Anwendung in AndroidManifest.xml:

<application […] android:theme="@style/MyTheme">… 
0
<style name="WelcomeTextViewTitleStyle"> 
     <item name="android:textSize">@dimen/welcome_textview_title_textsize</item> 
     <item name="android:layout_width">wrap_content</item> 
     <item name="android:layout_height">wrap_content</item> 
     <item name="android:gravity">center</item> 
     <item name="android:textStyle">bold</item> 
     <item name="android:textColor">@color/white</item> 

    </style> 
  1. gelten
  2. in Werte Ordner in Ihrem Textview einen gemeinsamen Stil in style.xml erstellen
<TextView android:id="@+id/tv_title2" 
      style="@style/WelcomeTextViewTitleStyle" 
      android:text="@string/wantToKnow" /> 
0

versuchen diesen Code

 <TextView android:text="assads" 
      android:textColor="#000000"/> 
    </TableRow> 
0

Wenn Sie dynamisch dann tun wollen nach Mytable in Java initialisiert und dann gehen Sie wie

for (int i = 0; i < mytable.getChildCount(); i++) { 
       View view = mytable.getChildAt(i); 
       if(view instanceof EditText)// 
       ((EditText)view).setTextColor(Color.BLACK); 
      } 
Verwandte Themen