Programming with eclipse, not recognizing my android code in XML
I'm brand new to eclipse and programming and I'm following video tutorials but I can't figure out why my eclipse doesn't recognizing the coding. In the video I see that he adds
android:gravity=center
android:background="@color/red"
and it changes color but mine does not..
the android:gravity
changes to a pink color and the =... changes to blue but mine stays black and when I look at the graphic layout the code is not registering
<?xml version="1.0" encoding="utf-开发者_开发问答8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
android:gravity=center
android:background="@color/red"
<TextView
android:layout_height="wrap_content"
android:text="@string/red" android:layout_width="wrap_content"/>
</LinearLayout>
Here is the corrected Code,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center"
android:background="#ff0000"
>
<TextView
android:layout_height="wrap_content"
android:text="@string/red"
android:layout_width="wrap_content"
/>
</LinearLayout>
Color can be also defined in XML files. Create a new xml file name color.xml inside /res/values. Paste the following code inside
<color name="red">#FF0000</color>
Also Make sure you have the following line in strings.xml
<string name="red">Your text</string>
and change the code as follows.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center"
android:background="@color/red"
>
<TextView
android:layout_height="wrap_content"
android:text="@string/red"
android:layout_width="wrap_content"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
-----------------
android:gravity=center
android:background="@color/red"
-----------------
That bit is wrong. It has be declared inside the layout object. Remove it.
<TextView
android:layout_height="wrap_content"
android:text="@string/red" android:layout_width="wrap_content"/>
</LinearLayout>
Have you declared the color red in your resource folder under string xml file?
Read
Missing the quotes in
android:gravity="center"
You need to create a color resource:
<color name="red">#FF0000</color>
You can find the documentation for the color resource here: http://developer.android.com/guide/topics/resources/more-resources.html
精彩评论