我正在做hellolinearlayout教程,但是使用字符串资源,而不是像教程那样将字符串直接硬编码到xml中。当我使用字符串资源运行应用程序时,它会立即崩溃。当我将字符串硬编码到xml代码中时,一切正常。关于我的应用程序崩溃的原因有什么想法吗?谢谢
MIN

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_weight="1">
            <TextView
                android:text="@string/box1text"
                android:gravity="center_horizontal"
                android:background="@string/box1color"
                android:layout_height="fill_parent"
                android:layout_width="wrap_content"
                android:layout_weight="@string/box1weight"
                />
            <TextView
                android:text="@string/box2text"
                android:gravity="center_horizontal"
                android:background="@string/box2color"
                android:layout_height="fill_parent"
                android:layout_width="wrap_content"
                android:layout_weight="@string/box2weight"
                />
            <TextView
                android:text="@string/box3text"
                android:gravity="center_horizontal"
                android:background="@string/box3color"
                android:layout_height="fill_parent"
                android:layout_width="wrap_content"
                android:layout_weight="@string/box2weight"
                />
            <TextView
                android:text="@string/box4text"
                android:gravity="center_horizontal"
                android:background="@string/box4color"
                android:layout_height="fill_parent"
                android:layout_width="wrap_content"
                android:layout_weight="@string/box4weight"
                />
            </LinearLayout>

StrugsXML
<?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="hello">Hello World, HelloLinearLayoutActivity!</string>
        <string name="app_name">HelloLinearLayout</string>
        <string name="box1text">red</string>
        <string name="box1color">#aa0000</string>
        <string name="box1weight">1</string>
        <string name="box2text">green</string>
        <string name="box2color">#00aa00</string>
        <string name="box2weight">1</string>
        <string name="box3text">blue</string>
        <string name="box3color">#0000aa</string>
        <string name="box3weight">1</string>
        <string name="box4text">yellow</string>
        <string name="box4color">#aaaa00</string>
        <string name="box4weight">1</string>
    </resources>

hellolinerlayoutactivity.java
package com.example.hellolinearlayout;

import android.app.Activity;
import android.os.Bundle;

public class HelloLinearLayoutActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

最佳答案

不能将背景色设置为字符串。
在res/values/colors.xml处创建XML文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <color name="opaque_red">#f00</color>
   <color name="translucent_red">#80ff0000</color>
  <color name="box4color">#aaaa00</color>
</resources>

然后用下面这样的。
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/box4color"
    android:textColor="@color/translucent_red"
    android:text="Hello"/>

10-07 19:47
查看更多