【问题标题】:TextView from XML and from code difference来自 XML 和来自代码差异的 TextView
【发布时间】:2012-09-09 08:53:20
【问题描述】:

当我从 XML 创建 TextView 时,里面的所有文本都是黑色的,但是当我从代码中创建它时,它是灰色的。为什么?

代码和 XML 中元素的位置相同:主要布局是 RelativeLayout(始终来自 XML)。

以下所有元素在一种情况下由 XML 创建,在第二种情况下由代码创建:ScrollView -> TableLayout -> TableRow -> TextView。

在所有层次结构的这两种情况下,我都没有设置颜色/样式/外观参数。

代码:

RelativeLayout layout = (RelativeLayout) findViewById(R.id.o_layout);
ScrollView sv = new ScrollView(getApplicationContext());
TableLayout tl = new TableLayout(getApplicationContext());
float density = getResources().getDisplayMetrics().density;
tl.setPadding((int)(10 * density), (int)(4 * density), (int)(4 * density), 0);
for(String s: lista){
     add_row(tl, s);
}
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.osobowe_search_field);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
sv.addView(tl);
layout.addView(sv, params);

private void add_row(TableLayout tl, String left_text){
    float density = getResources().getDisplayMetrics().density;

    TableRow tr = new TableRow(getApplicationContext());
    tr.setMinimumHeight((int)(30 * density));
    TableLayout.LayoutParams params = new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.bottomMargin = (int)(4 * density);

    TextView left = new TextView(getApplicationContext());
    left.setTextSize(android.util.TypedValue.COMPLEX_UNIT_DIP, 25);
    TableRow.LayoutParams left_params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
    left_params.gravity = Gravity.CENTER_VERTICAL;
    left.setText(left_text);

    TextView right = new TextView(getApplicationContext());
    right.setTextSize(android.util.TypedValue.COMPLEX_UNIT_DIP, 18);
    right.setGravity(Gravity.CENTER_VERTICAL);
    TableRow.LayoutParams right_params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
    right_params.leftMargin = (int)(10 * density);
    right_params.gravity = Gravity.CENTER_VERTICAL;
    //right_params.setMargins((int)(10 * density), 0, 0, 0);
    right_params.weight = 1;
    right.setText(get_desc("l", left_text));

    View line = new View(getApplicationContext());
    line.setBackgroundResource(R.drawable.line_on_bottom);
    TableLayout.LayoutParams line_params = new TableLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, (int)density);
    line_params.setMargins((int)(20 * density), 0, (int)(20 * density), 0);

    tr.addView(left, left_params);
    tr.addView(right, right_params);
    tl.addView(tr, params);
    tl.addView(line, line_params);


}

XML:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/o_details"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dip"
        android:paddingRight="4dip"
        android:paddingTop="4dip" >

        <TableRow
            android:id="@+id/o_row01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minHeight="30dp"
            android:layout_marginBottom="4dip" >

            <TextView
                android:id="@+id/o_num01"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:text="@string/_"
                android:textSize="25dip"
                />

【问题讨论】:

  • TextView 在 XML 和代码中的位置?

标签: android android-layout textview


【解决方案1】:

一个简单的解决方法如下—— 尝试在 onResume() 方法或 add-row 函数(上面的代码)中使用其已知 ID(根据上面的 xml)检索 TextView,并手动将颜色设置为黑色/灰色。功能 setBackgroundColor 会有所帮助。

用法--

mytextview.setBackgroundColor(getResources().getColor(R.color.darkgrey)); 

(如果您在资源/color.xml 中使用其十六进制值定义了颜色“darkgrey”)

【讨论】:

  • 我知道我可以改变颜色,但我不认为这是解决方案。我想知道为什么它的行为与我描述的一样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-09
  • 2019-06-14
  • 1970-01-01
  • 2012-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多