在TableLayout中优化文本视图颜色

时间:2014-01-14 18:31:47

标签: java android xml tablelayout textcolor

作为一个一年或两年的程序员(不过更新的Android和XML),必须有一种更简单的方法来实现这一点,或者优化它。

基本上,我想在我的TableLayout颜色中将TextViews的颜色设置为黑色,显然你必须通过JAVA而不是XML来实现。继承我的代码,它有效,但似乎很多。有没有办法通过循环或任何东西来缩小它?

CLASS FILE

public class TabsSterile extends Activity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sterileconditioners);

    TextView tv1 = (TextView) findViewById(R.id.tv1);
    tv1.setTextColor(0xFF000000);

    TextView tv2 = (TextView) findViewById(R.id.tv2);
    tv2.setTextColor(0xFF000000);

    TextView tv3 = (TextView) findViewById(R.id.tv3);
    tv3.setTextColor(0xFF000000);

    TextView tv4 = (TextView) findViewById(R.id.tv4);
    tv4.setTextColor(0xFF000000);

    TextView tv5 = (TextView) findViewById(R.id.tv5);
    tv5.setTextColor(0xFF000000);

    TextView tv6 = (TextView) findViewById(R.id.tv6);
    tv6.setTextColor(0xFF000000);

    TextView tv7 = (TextView) findViewById(R.id.tv7);
    tv7.setTextColor(0xFF000000);

    TextView tv8 = (TextView) findViewById(R.id.tv8);
    tv8.setTextColor(0xFF000000);

    TextView tv9 = (TextView) findViewById(R.id.tv9);
    tv9.setTextColor(0xFF000000);

    TextView tv10 = (TextView) findViewById(R.id.tv10);
    tv10.setTextColor(0xFF000000);

    TextView tv11 = (TextView) findViewById(R.id.tv11);
    tv11.setTextColor(0xFF000000);

    TextView tv12 = (TextView) findViewById(R.id.tv12);
    tv12.setTextColor(0xFF000000);

    TextView tv13 = (TextView) findViewById(R.id.tv13);
    tv13.setTextColor(0xFF000000);

    TextView tv14 = (TextView) findViewById(R.id.tv14);
    tv14.setTextColor(0xFF000000);

    TextView tv15 = (TextView) findViewById(R.id.tv15);
    tv15.setTextColor(0xFF000000);

    TextView tv16 = (TextView) findViewById(R.id.tv16);
    tv16.setTextColor(0xFF000000);

    TextView tv17 = (TextView) findViewById(R.id.tv17);
    tv17.setTextColor(0xFF000000);

    TextView tv18 = (TextView) findViewById(R.id.tv18);
    tv18.setTextColor(0xFF000000);

    TextView tv19 = (TextView) findViewById(R.id.tv19);
    tv19.setTextColor(0xFF000000);

    TextView tv20 = (TextView) findViewById(R.id.tv20);
    tv20.setTextColor(0xFF000000);

    TextView tv21 = (TextView) findViewById(R.id.tv21);
    tv21.setTextColor(0xFF000000);

    TextView tv22 = (TextView) findViewById(R.id.tv22);
    tv22.setTextColor(0xFF000000);

    TextView tv23 = (TextView) findViewById(R.id.tv23);
    tv23.setTextColor(0xFF000000);

    TextView tv24 = (TextView) findViewById(R.id.tv24);
    tv24.setTextColor(0xFF000000);

    TextView tv25 = (TextView) findViewById(R.id.tv25);
    tv25.setTextColor(0xFF000000);

    TextView tv26 = (TextView) findViewById(R.id.tv26);
    tv26.setTextColor(0xFF000000);

    TextView tv27 = (TextView) findViewById(R.id.tv27);
    tv27.setTextColor(0xFF000000);

    TextView tv28 = (TextView) findViewById(R.id.tv28);
    tv28.setTextColor(0xFF000000);

    TextView tv29 = (TextView) findViewById(R.id.tv29);
    tv29.setTextColor(0xFF000000);

    TextView tv30 = (TextView) findViewById(R.id.tv30);
    tv30.setTextColor(0xFF000000);

    }
}

XML文件     

<TableLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#1155ff" >
    <TableRow android:layout_margin="1dp" android:background="#77bbff">
        <TextView android:text="Model"
            android:id="@+id/tv1" />
        <TextView android:text="Tank Capacity (gal)"
            android:id="@+id/tv2" />
    </TableRow>
    <TableRow android:layout_margin="1dp" android:background="#ffffff">
        <TextView android:text="FG 2010 Sterile Conditioner"
            android:id="@+id/tv3" />
        <TextView android:text="Up to 5,000"
            android:id="@+id/tv4" />
    </TableRow>
    <TableRow android:layout_margin="1dp" android:background="#ffffff">
        <TextView android:text="FG 2030 Sterile Conditioner"
            android:id="@+id/tv5" />
        <TextView android:text="Up to 5,000 w/ HEPA"
            android:id="@+id/tv6" />
    </TableRow>
    <TableRow android:layout_margin="1dp" android:background="#ffffff">
        <TextView android:text="FG 2036 Sterile Conditioner"
            android:id="@+id/tv7" />
        <TextView android:text="5,000 to 7,000"
            android:id="@+id/tv8" />
    </TableRow>
    <TableRow android:layout_margin="1dp" android:background="#ffffff">
        <TextView android:text="FG 2072HF Sterile Conditioner"
            android:id="@+id/tv9" />
        <TextView android:text="7,000 to 18,000"
            android:id="@+id/tv10" />
    </TableRow>
    <TableRow android:layout_margin="1dp" android:background="#ffffff">
        <TextView android:text="FG 2108-2020 Sterile Conditioner"
            android:id="@+id/tv11" />
        <TextView android:text="18,000 to 40,000"
            android:id="@+id/tv12" />
    </TableRow>
    <TableRow android:layout_margin="1dp" android:background="#ffffff">
        <TextView android:text="FG 2144-2020 Sterile Conditioner"
            android:id="@+id/tv13" />
        <TextView android:text="40,000 to 100,000"
            android:id="@+id/tv14" />
    </TableRow>
    <TableRow android:layout_margin="1dp" android:background="#ffffff">
        <TextView android:text="FG 2200 Sterile Conditioner"
            android:id="@+id/tv15" />
        <TextView android:text="100,000 +"
            android:id="@+id/tv16" />
    </TableRow>
    <TableRow android:layout_margin="1dp" android:background="#ffffff">
        <TextView android:text="Consult Fuller"
            android:id="@+id/tv17" />
        <TextView android:text="100,000 +"
            android:id="@+id/tv18" />
    </TableRow>
    <TableRow android:layout_margin="1dp" android:background="#ffffff">
        <TextView android:text="FG 2001 Air Vent"
            android:id="@+id/tv19" />
        <TextView android:text="Up to 18,000"
            android:id="@+id/tv20" />
    </TableRow>
    <TableRow android:layout_margin="1dp" android:background="#ffffff">
        <TextView android:text="FG 2002 Air Vent"
            android:id="@+id/tv21" />
        <TextView android:text="40,000 +"
            android:id="@+id/tv22" />
    </TableRow>
    <TableRow android:layout_margin="1dp" android:background="#ffffff">
        <TextView android:text="FG 2003 Air Vent"
            android:id="@+id/tv23" />
        <TextView android:text="40,000 +"
            android:id="@+id/tv24" />
    </TableRow>
    <TableRow android:layout_margin="1dp" android:background="#ffffff">
        <TextView android:text="Central Systems"
            android:id="@+id/tv25" />
        <TextView android:text="Consult Fuller"
            android:id="@+id/tv26" />
    </TableRow>
    <TableRow android:layout_margin="1dp" android:background="#ffffff">
        <TextView android:text="Air Circulators"
            android:id="@+id/tv27" />
        <TextView android:text="Consult Fuller"
            android:id="@+id/tv28" />
    </TableRow>
    <TableRow android:layout_margin="1dp" android:background="#ffffff">
        <TextView android:text="Tank Head Space Fixtures"
            android:id="@+id/tv29" />
        <TextView android:text="Consult Fuller"
            android:id="@+id/tv30" />
    </TableRow>
</TableLayout>

</ScrollView>

3 个答案:

答案 0 :(得分:1)

一种方法是使用这样的数组,假设我有3个textviews-

//在类级声明数组

    textArray = new TextView[3];
    textView[0] = (TextView) findViewById(R.id.tv1);
    textView[1] = (TextView) findViewById(R.id.tv2);
    textView[2] = (TextView) findViewById(R.id.tv2);

for (int i = 0; i < textArray .length; i++) 
{
    textView[i].setTextColor(0xFF000000);   
}

答案 1 :(得分:0)

您可以使用android:textColor =“#000000”在xml中设置文本颜色。 有关所有属性的列表,请参阅TextView文档。

答案 2 :(得分:0)

我建议使用自定义样式或主题。

这将允许您只定义文本属性一次,并自动将它们应用于所有视图(主题)或选择性地通过样式。 所有这些都可以用XML完成,而无需编写附加的Java代码。

这将帮助您入门:http://www.vogella.com/tutorials/AndroidStylesThemes/article.html